Let S be the subset of the set of ordered pairs of integers defined recursively by
Basis step: (0, 0) ∈ S.
Recursive step: If (a,b) ∈ S,
then (a,b + 1) ∈ S, (a + 1, b + 1) ∈ S, and (a + 2, b + 1) ∈ S.
List the elements of S produced by the first four application
def subset(a,b):
base=[]
if base == []:
base.append((a,b))
return base
elif (a,b) in base:
base.append(subset(a,b+1))
base.append(subset(a+1,b+1))
base.append(subset(a+2,b+1))
return base
for number in range(0,5):
for number2 in range(0,5):
print(*subset(number,number2))
The output is
(0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(1, 0)
(1, 1)
(1, 2)
(1, 3)
(1, 4)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 0)
(3, 1)
(3, 2)
(3, 3)
(3, 4)
(4, 0)
(4, 1)
(4, 2)
(4, 3)
(4, 4)
But the correct answer is more than what I got.
(0, 1), (1, 1), and (2, 1) are all in S. If we apply the recursive step to these we add (0, 2), (1, 2), (2, 2), (3, 2), and (4, 2). The next round gives us (0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), and (6, 3). And a fourth set of applications adds (0,4), (1,4), (2,4), (3,4), (4,4), (5,4), (6,4), (7,4), and (8,4).
What did I do wrong with my code?
Is this what you want ? Based on the result you wanted :
def subset(a):
#Returns a list that contains all (i, a + 1) from i = 0 to i = (a + 1) * 2 + 1
return [(i, a + 1) for i in range((a + 1) * 2 + 1)]
setList = []
for i in range(4):
setList += subset(i) #Fill a list with subset result from i = 0 -> 3
print(setList)
If you want to use a recursive function, you can do that too :
def subset(a, b):
if a < b * 2:
#Returns a list that contains (a, b) and unzipped result of subset(a + 1, b)
#Otherwise it would add a list in the list
return [(a, b), *subset(a + 1, b)]
else:
return [(a, b)] #If deepest element of recursion, just return [(a, b)]
setList = []
for i in range(5):
setList += subset(0, i)
print(setList)
Related
So I am trying to refresh my knowledge. I tried finding the answer to this question here and I am not still not sure how to answer this.
Basically if I have a 2D index array (call it a) of shape (x, z) and a 3D array (call it b) of shape (x, y, z), then I want a 2D array (call it c) which has the property that the i,j element of c, c[i, j] = b[i, a[i, j], j]. Shape of c is (x, z)
To summarize:
a.shape = (x, z)
b.shape = (x, y, z)
c[i, j] = b[i, a[i, j], j]
c.shape = (x, z)
I tried to use the following code, but did not get the results I desire.
import numpy as np
input = numpy.random.randn(3, 5, 10)
target_np = np.random.randint(high = 5, low = 1, size = (3, 10))
q = input[:, np.ravel(target_np),:]
print(q.shape)
The result was
(3, 30, 10)
I want to do a product of a matrix M x N (M timestamps with N fractures each) and a vector M x 1 to get a vector of size N x 1, so the timestamp dimension is eliminated. How can I do that?
An example:
import keras.backend as K
a = K.constant([[1,2,3],[1,2,3]])
print(a.shape)
b = K.constant([[1],[2]])
print(b.shape)
result = K.dot(K.transpose(a),b)
print(result.shape)
print(K.eval(result))
# print
(2, 3)
(2, 1)
(3, 1)
[[3.]
[6.]
[9.]]
I want to be able to filter the below data so I can find specific data for example if I wanted to find an item with only apples it would look similar to this output: [("apple","crate",6),("apple","box",3)]
fruit :: [(String, String, Int)]
fruit = [("apple", "crate", 6), ("pear", "crate", 5), ("mango", "box", 4),
("apple", "box", 3), ("banana", "box", 5), ("pear", "box", 10), ("apricot",
"box", 4), ("peach", "box", 5), ("walnut", "box", 4), ("blueberry", "tray", 10),
("blackberry", "tray", 4), ("watermelon", "piece", 8), ("marrow", "piece", 7),
("hazelnut", "sack", 2), ("walnut", "sack", 4)]
first :: (a, b, c) -> a
first (x, _, _) = x
second :: (a, b, c) -> b
second (_, y, _) = y
third :: (a, b, c) -> c
third (_, _, z) = z
A couple of alternatives:
filter ((=="apple") . first) fruit
[ f | f#("apple",_,_) <- fruit ]
The first one exploits your first projection, checking whether its result is equal to "apple".
The second one instead exploits list comprehensions, where elements that fail to pattern match are discarded.
Perhaps an even more basic approach is using a lambda abstraction and equality.
filter (\(s,_,_) -> s == "apple") fruit
Suppose
rdd1 = ( (a, 1), (a, 2), (b, 1) ),
rdd2 = ( (a, ?), (a, *), (c, .) ).
Want to generate
( (a, (1, ?)), (a, (1, *)), (a, (2, ?)), (a, (2, *)) ).
Any easy methods?
I think it is different from the cross join but can't find a good solution.
My solution is
(rdd1
.cartesian( rdd2 )
.filter( lambda (k, v): k[0]==v[0] )
.map( lambda (k, v): (k[0], (k[1], v[1])) ))
You are just looking for a simple join, e.g.
rdd = sc.parallelize([("red",20),("red",30),("blue", 100)])
rdd2 = sc.parallelize([("red",40),("red",50),("yellow", 10000)])
rdd.join(rdd2).collect()
# Gives [('red', (20, 40)), ('red', (20, 50)), ('red', (30, 40)), ('red', (30, 50))]
I am having a letter mapped with its respective score
dict = fromList([("A",1), ("B",3), ("C", 3), ("E", 1), ("D", 2), ("G", 2), ("F", 4), ("I", 1), ("H", 4), ("K", 5), ("J", 8), ("M", 3), ("L", 1), ("O", 1), ("N", 1), ("Q", 10), ("P", 3), ("S", 1), ("R", 1), ("U", 1), ("T", 1), ("W", 4), ("V", 4), ("Y", 4), ("X", 8), ("Z", 10)])
If the main send a word to the function the function should return the score with respect to the dict and the letters in the word.
EX:- Main :- APPLE
Function should return :- 9
(A Score)1+(P Score)3 +(P Score)3 + (L Score) 1+(E Score)1 = 9
You could use lookup to create a function that maps keys to values:
mapper :: Eq k => [(k, v)] -> k -> v
mapper dict k = case lookup k dict of Nothing -> undefined
(Just v) -> v
scrabble :: Char -> Int
scrabble = mapper [ ('A', 1)
, ('B', 3)
, ('C', 3)
, ('E', 1)
, ('D', 2)
, ('G', 2)
, ('F', 4)
, ('I', 1)
, ('H', 4)
, ('K', 5)
, ('J', 8)
, ('M', 3)
, ('L', 1)
, ('O', 1)
, ('N', 1)
, ('Q', 10)
, ('P', 3)
, ('S', 1)
, ('R', 1)
, ('U', 1)
, ('T', 1)
, ('W', 4)
, ('V', 4)
, ('Y', 4)
, ('X', 8)
, ('Z', 10)
]
Now all you need to do is create a function which takes a string and returns its score:
score :: String -> Int
score = sum . map scrabble
main = print $ score "APPLE"
That's all.
Edit: There's nothing wrong with returning undefined in mapper when a lookup fails. If you need error handling you could simply define mapper as flip lookup and hey presto scrabble is now of type Char -> Maybe Int.
Consider how you would write scrabble using pattern matching:
scrabble :: Char -> Int
scrabble 'A' = 1
scrabble 'B' = 3
scrabble 'C' = 3
scrabble 'D' = 2
scrabble 'E' = 1
scrabble 'F' = 4
scrabble 'G' = 2
scrabble 'H' = 4
scrabble 'I' = 1
scrabble 'J' = 8
scrabble 'K' = 5
scrabble 'L' = 1
scrabble 'M' = 3
scrabble 'N' = 1
scrabble 'O' = 1
scrabble 'P' = 3
scrabble 'Q' = 10
scrabble 'R' = 1
scrabble 'S' = 1
scrabble 'T' = 1
scrabble 'U' = 1
scrabble 'V' = 4
scrabble 'W' = 4
scrabble 'X' = 8
scrabble 'Y' = 4
scrabble 'Z' = 10
If the pattern match fails then you end up with a bottom value anyway. This is not a problem if you know that the pattern match will never fail. If you do need to handle failures then simply use flip lookup as I mentioned above.
main = print $ calWordScore "APPLE"
calcWordScore :: String -> Int
calcWordScore word = sum $ map calcLetterScore word
calcLetterScore :: Char -> Int
calcLetterScore ch = Map.fromList([('A',1), ('B',3), ('C', 3), ('E', 1), ('D', 2), ('G', 2), ('F', 4), ('I', 1), ('H', 4), ('K', 5), ('J', 8), ('M', 3), ('L', 1), ('O', 1), ('N', 1), ('Q', 10), ('P', 3), ('S', 1), ('R', 1), ('U', 1), ('T', 1), ('W', 4), ('V', 4), ('Y', 4), ('X', 8), ('Z', 10)]) Map.! ch
And you need to
import qualified Data.Map.Lazy as Map