In [ ]:
# -----------------------------Question1------------------------------------
def rev(str):
return ' '.join(str.split()[::-1])
print(rev("My name is John"))
In [ ]:
#------------------------Question2: One Liner---------------------------------
print(["Fizz"*(not i%3) + "Buzz"*(not i%5) or i for i in range(1, 100)])
In [ ]:
# -----------------------------Question3------------------------------------
set1 = set(input().split(','))
set2 = set(input().split(','))
print(set1.union(set2).difference(set1.intersection(set2)))
In [ ]:
#------------------------------Question4------------------------------------
# This import statement is for plotting. You will need matplotlib installed.
import matplotlib.pyplot as plt
lst = [(1, 1), (2, 2), (3, 3), (4, 6)]
n = len(lst)
x_i = [lst[i][0] for i in range(n)]
y_i = [lst[i][1] for i in range(n)]
product_sum = sum([x_i[i] * y_i[i] for i in range(n)])
x_sum = sum(x_i)
y_sum = sum(y_i)
x_squared_sum = sum([x_coord**2 for x_coord in x_i])
denom = n * x_squared_sum - x_sum ** 2
if denom != 0:
slope = (n * product_sum - x_sum * y_sum) / denom
y_intercept = y_sum / n - slope * x_sum / n
best_fit_y = [slope * x + y_intercept for x in x_i]
print("The best fit line is : ", " y = ", slope, "x + ", y_intercept)
else:
x_mean = x_sum / n
best_fit_y = [y for y in y_i]
print("The best fit line is : ", " x = ", x_mean)
# This is for plotting, you needn't understand this as of now.
plt.plot(x_i, best_fit_y, '--')
plt.plot(x_i, y_i, 'ro')
plt.show()
In [ ]:
# Question 5
#---------------Your code starts here --------------------
if ball_pos[1] <= BALL_RADIUS or ball_pos[1] >= (HEIGHT - 1 ) - BALL_RADIUS:
ball_vel[1] = -1 * ball_vel[1]
if ball_pos[0] <= BALL_RADIUS or ball_pos[0] >= (WIDTH - 1 ) - BALL_RADIUS:
ball_vel[0] = -1 * ball_vel[0]
#---------------End of your code -------------------------
In [ ]:
#-------------------------------Bonus Question----------------------------
import string
from random import *
characters = string.ascii_letters + string.punctuation + string.digits
password = "".join(choice(characters) for x in range(randint(8, 16)))
print (password)