For example I have next RDD of the type ((i,j), k):
((0,0), 0)
((0,1), 0)
((1,0), 0)
((1,1), 0)
I want to transform it to another one, which has 1 if i==j. My first attempt is going wrong:
rddnew = rdd.flatMap(lambda ((i,j), k): [if i==j: ((i,j), 1)]))
Can somebody help me to improve this code in python?
Here is a solution :
data = [((0, 0), 0), ((0, 1), 0), ((1, 0), 0), ((1, 1), 0)]
rdd = sc.parallelize(data)
rdd2 = rdd.map(lambda ((i, j), _): ((i, j), 1) if (i == j) else ((i, j), 0))
rdd2.collect()
# [((0, 0), 1), ((0, 1), 0), ((1, 0), 0), ((1, 1), 1)]
You can also define a cleaner solution by using a function on the mapper :
def transformation(entry):
(i, j), v = entry
return (i, j), v + 1 if i == j else 0
rdd3 = rdd.map(transformation)
rdd3.collect()
# [((0, 0), 1), ((0, 1), 0), ((1, 0), 0), ((1, 1), 1)]
Related
I'm watching a tutorial on how to make Tetris and I'm completely lost trying to understand how to represent the different rotations of pieces.
The tutorial references the 2009 Tetris Design Guide, particularly the slide on Tetrimino Facings:
Relevant Code in the video starts at 1:10:27
The below code represent tuples indicating coordinates at which every piece rotates around.
Kind::O => &[(0, 0), (0, 1), (1, 0), (1, 1)],
Kind::I => &[(-1, 0), (0, 0), (1, 0), (2, 0)],
Kind::T => &[(-1, 0), (0, 0), (1, 0), (0, 1)],
Kind::L => &[(-1, 0), (0, 0), (1, 0), (1, 1)],
Kind::J => &[(-1, 1), (-1, 0), (0, 0), (1, 0)],
Kind::S => &[(-1, 0), (0, 0), (0, 1), (1, 1)],
Kind::Z => &[(-1, 1), (0, 1), (0, 0), (1, 0)],
Can someone explain to me this representation system please?
Example with J [(-1, 1), (-1, 0), (0, 0), (1, 0)]:
(y)
3
2
1 A
0 B C D
-1
-1 0 1 2 (x)
where
A = (-1, 1)
B = (-1, 0)
C = ( 0, 0)
D = ( 1, 0)
(0,0) (here C) is the center of rotation.
To rotate, do this transform for each block :
clockwise: (x, y) => (-y, x)
anticlockwise: (x, y) => (y, -x)
raise LayoutError(ident)
reportlab.platypus.doctemplate.LayoutError:
Flowable <Table#0x7FA13491B898 1 rows x 1 cols(tallest row 900)> with cell(0,0) containing
'<Table#0x7FA1349110B8 1 rows x 1 cols(tallest row 894)> with cell(0,0) containing\n\'<Table#0x7FA1349894E0 1 rows x 1 cols(tallest row 24)> with cell(0,0) containing\\n"<Table#0x7FA134989438 1 rows x 1 cols(tallest row 18)> with cell(0,0) containing\\\\n\\\'Vulnerability ( Virustotal Domain Report )\\\'"\''(583.2755905511812 x 900), tallest cell 900.0 points, too large on page 6 in frame 'normal'(583.2755905511812 x 794.8897637795277*) of template 'Later'
I make a PDF(A4) size for it and got the error, while Ienter code here make A3 for it the problem is solved, i want the solution for A4 size.
def getToolsTables(data, tname):
doc = SimpleDocTemplate(
"somefilename.pdf",
pagesize=A2,
topMargin=25,
bottomMargin=0,
)
main_header = []
h_arr1 = []
h_arr1.append(tname)
main_header.append(h_arr1)
mainHeader = Table(main_header, colWidths="*")
finalTable = []
main_table_header_Style = TableStyle(
[
("BACKGROUND", (0, 0), (-1, 0), "#D3D3D3"),
("TEXTCOLOR", (0, 0), (-1, -1), colors.black),
("ALIGN", (0, 0), (-1, -1), "LEFT"),
("FONTSIZE", (0, 0), (-1, -1), 12),
("FONTNAME", (0, 0), (-1, -1), "Courier-Bold"),
("TOPPADDING", (0, 0), (-1, -1), 5),
("BOTTOMPADDING", (0, 0), (-1, -1), 7),
("LINEBELOW", (0, 0), (-1, 0), 1, colors.black),
]
)
tools_table_header_Style = TableStyle(
[
("BACKGROUND", (0, 0), (-1, 0), "lightblue"),
("TEXTCOLOR", (0, 0), (-1, -1), colors.black),
("ALIGN", (0, 0), (-1, -1), "LEFT"),
("FONTSIZE", (0, 0), (-1, -1), 11),
("FONTNAME", (0, 0), (-1, -1), "Courier-Bold"),
]
)
tools_table_header_Style1 = TableStyle(
[
("BACKGROUND", (0, 0), (-1, 0), "lightgreen"),
("TEXTCOLOR", (0, 0), (-1, -1), colors.black),
("ALIGN", (0, 0), (-1, -1), "CENTER"),
("FONTSIZE", (0, 0), (-1, -1), 11),
("FONTNAME", (0, 0), (-1, -1), "Courier-Bold"),
]
)
Style2 = TableStyle(
[
("ALIGN", (0, 0), (-1, -1), "LEFT"),
("LEFTPADDING", (0, 0), (-1, -1), 0),
("RIGHTPADDING", (0, 0), (-1, -1), 0),
("BOTTOMPADDING", (0, 0), (-1, -1), -10),
]
)
Style3 = TableStyle(
[
("VALIGN", (0, 0), (-1, -1), "TOP"),
("ALIGN", (0, 0), (-1, -1), "RIGHT"),
("LEFTPADDING", (0, 0), (-1, -1), 130),
("RIGHTPADDING", (0, 0), (-1, -1), 0),
("TOPPADDING", (0, 0), (-1, -1), 0),
]
)
Style4 = TableStyle([("VALIGN", (0, 0), (-1, -1), "TOP")])
mainHeader.setStyle(
main_table_header_Style
) # adding style to table main header
finalTable.append(mainHeader)
# Create Tools Array
tools_header = []
tools_body = []
if type(data) == dict:
all_table = []
for key, value in data.items():
temp = []
temp_table = []
temp.append(key)
tool_table_header = Table([temp], colWidths="*")
tool_table_header.setStyle(tools_table_header_Style)
temp_table.append(tool_table_header)
if key != "Builtwith":
t_h = []
t_b = []
for k, v in value.items():
t_h.append(k)
t_b.append(v)
t_body = []
for index, item in enumerate(t_h):
if item != "status":
arr1 = []
arr2 = []
if type(t_b[index]) is list:
temp_txt = ""
for txt in t_b[index]:
temp_txt += txt + ", "
arr1.append(item + ":")
text = t_b[index]
wraped_text = "\n".join(
wrap(str(temp_txt[:-3]), 60)
) # 60 is line width
arr1.append(wraped_text)
else:
# MY CODE
arr2.append(arr1)
n_table = Table(arr2, [200, 370])
n_table.setStyle(Style4)
t_body.append(n_table)
tool_header = Table([temp_table], colWidths="*")
tool_body = Table([[t_body]], [200, 370])
finalTable.append(
Table(
[[[tool_header, tool_body]]],
colWidths="*",
)
)
Use splitInRow argument in the Table class so that it will split the data on the other page, for example:
tool_body = Table([[t_body]], [200, 370], splitInRow=1)
Consider you are given n data points in the form of list of tuples like S=[(x1,y1),(x2,y2),(x3,y3),(x4,y4),(x5,y5),..,(xn,yn)] and a point P=(p,q)
your task is to find 5 closest points(based on cosine distance) in S from P
Ex:
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1)(6,0),(1,-1)]
P= (3,-4)
I have tried with below code
import math
data = [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1)(6,0),(1,-1)]
data.sort(key=lambda x: math.sqrt((float(x.split(",")[0]) - 3)**2 +
(float(x.split(",")[1]) -(-4))**2))
print(data)
I should get 5 closest points in S from P.
You have a missing comma in the defenition of data
You have a list of tuples but for some reason you used split as if it was a list of strings.
If you fix these 2 errors it works. You just need to grab the first 5 elements from data:
import math
data = [(1, 2), (3, 4), (-1, 1), (6, -7), (0, 6), (-5, -8), (-1, -1), (6, 0), (1, -1)]
data.sort(key=lambda x: math.sqrt((float(x[0]) - 3) ** 2 +
(float(x[1]) - (-4)) ** 2))
print(data[:5])
Outputs
[(1, -1), (6, -7), (-1, -1), (6, 0), (1, 2)]
(Next time, if you get an error please explain it in your question)
cosine_dist = []
for a, b in S:
num = a * P[0] + b * P[1]
den = math.sqrt(a * a + b * b) * math.sqrt(P[0] * P[0] + P[1] * P[1])
cosine_dist.append(math.acos(num/den))
X = cosine_dist
Y = [S for S in sorted(zip(S,X), key=lambda i:i[1])]
k = Y[:5]
for i, j in k:
print(i)
P = (3, -4)
S = [(1, 2), (3, 4), (-1, 1), (6, -7), (0, 6), (-5, -8), (-1, -1), (6, 0), (1, -1)]
How can I perform something like dtR (delete until you meet the R in the line) across all the lines in a linewise visual block, or say, for the next n lines?
The long story
Let's suppose there is the following piece of code:
L0 = Rotated_Shape((1, 0), (-1, 0), (-1, 1))
L90 = Rotated_Shape((0, -1), (0, 1), (1, 1))
L180 = Rotated_Shape((-1, 0), (1, 0), (1, -1))
L270 = Rotated_Shape((0, 1), (0, -1), (-1, -1))
O0 = Rotated_Shape((1, 0), (1, -1), (0, -1))
O90 = Rotated_Shape((0, -1), (-1, -1), (-1, 0))
O180 = Rotated_Shape((-1, 0), (-1, 1), (0, 1))
O270 = Rotated_Shape((0, 1), (1, 1), (1, 0))
S0 = Rotated_Shape((-1, 0), (0, -1), (1, -1))
S90 = Rotated_Shape((0, 1), (-1, 0), (-1, -1))
S180 = Rotated_Shape((1, 0), (0, 1), (-1, 1))
S270 = Rotated_Shape((0, -1), (1, 0), (1, 1))
T0 = Rotated_Shape((0, 1), (-1, 0), (1, 0))
T90 = Rotated_Shape((1, 0), (0, 1), (0, -1))
T180 = Rotated_Shape((0, -1), (1, 0), (-1, 0))
T270 = Rotated_Shape((-1, 0), (0, -1), (0, 1))
Z0 = Rotated_Shape((1, 0), (0, -1), (-1, -1))
Z90 = Rotated_Shape((0, -1), (-1, 0), (-1, 1))
Z180 = Rotated_Shape((-1, 0), (0, 1), (1, 1))
Z270 = Rotated_Shape((0, 1), (1, 0), (1, -1))
I am now reworking this code and I want all these lines to become what is on the right side of the equation (assignment), i. e. delete the variable names, the equal sign and keep only the Rotated_Shape((... part.
I guess the best way to do this particular case is to search and replace a regular expression, but my first approach I didn't know how to do was to use the blockwise-visual or line-visual mode for each four of them and do something like dtR (delete until you meet the R in the line).
You can highlight those lines with a visual block and then use :help 'normal'.
The end result post-select looks something like: :'<,'>normal dtR
You can also abbreviate normal to norm.
I am trying to write a function that will help me break down a fraction into simple components like so
I wrote this function
from fractions import Fraction
def lowest_fraction(frac):
num = frac[0]
den = frac[-1]
if frac == (1, den):
yield frac
while frac != (1, den):
new_num = num - num//2
rem = num - new_num
new_frac = Fraction(new_num, den)
frac = (new_frac.numerator, new_frac.denominator)
num = rem
yield frac
I ran the below
for each in [(1, 2), (3, 4), (3, 8), (5, 8), (7, 8), (7, 16), (9, 32)]:
print('{}: '.format(each))
for k in lowest_fraction(each):
print('\t{}'.format(k))
Here's the output
(1, 2):
(1, 2)
(3, 4):
(1, 2)
(1, 4)
(3, 8):
(1, 4)
(1, 8)
(5, 8):
(3, 8)
(1, 8)
(7, 8):
(1, 2)
(1, 4)
(1, 8)
(7, 16):
(1, 4)
(1, 8)
(1, 16)
(9, 32):
(5, 32)
(1, 16)
(1, 32)
You can see that the output for (3, 8) and (9, 32) is not in their simplest form. I think what I want to do can be accomplished by writing a recursive code but I cannot presently write one because I don't fully grab the concept of recursion yet.