In: Computer Science
def isPower(x, y):
"""
>>> isPower(4, 64)
3
>>> isPower(5, 81)
-1
>>> isPower(7, 16807)
5
"""
# --- YOU CODE STARTS HERE
def isPower(x, y):
num = x
count = 1
while x < y:
x *= num
count += 1
if x == y:
return count
return -1
def translate(translation, txt):
"""
>>> myDict = {'up': 'down', 'down': 'up', 'left': 'right', 'right': 'left', '1': 'one'}
>>> text = 'Up down left Right forward 1 time'
>>> translate(myDict, text)
'down up right left forward one time'
"""
# --- YOU CODE STARTS HERE
def translate(translationDict, txt):
words = txt.lower().split()
for i in range(len(words)):
if words[i] in translationDict:
words[i] = translationDict[words[i]]
return ' '.join(words)
def onlyTwo(x, y, z):
"""
>>> onlyTwo(1, 2, 3)
13
>>> onlyTwo(3, 3, 2)
18
>>> onlyTwo(5, 5, 5)
50
"""
# --- YOU CODE STARTS HERE
def onlyTwo(x, y, z):
m1 = max(x, y, z)
m2 = (x + y + z) - min(x, y, z) - m1
return m1 * m1 + m2 * m2
def largeFactor(num):
"""
>>> largeFactor(15)
5
>>> largeFactor(24)
12
>>> largeFactor(7)
1
"""
# --- YOU CODE STARTS HERE
def largeFactor(num):
n = num - 1
while n >= 1:
if num % n == 0:
return n
n -= 1
def hailstone(num):
"""
>>> hailstone(5)
[5, 16, 8, 4, 2, 1]
>>> hailstone(6)
[6, 3, 10, 5, 16, 8, 4, 2, 1]
"""
# --- YOU CODE STARTS HERE
def hailstone(num):
result = []
while num > 1:
result.append(num)
if num % 2 == 0:
num = num // 2
else:
num = 3 * num + 1
result.append(num)
return result
if any wrong with my code
def isPower(x, y):
"""
>>> isPower(4, 64)
3
>>> isPower(5, 81)
-1
>>> isPower(7, 16807)
5
"""
num = x
count = 1
while x < y:
x *= num
count += 1
if x == y:
return count
return -1
def translate(translation, txt):
"""
>>> myDict = {'up': 'down', 'down': 'up', 'left': 'right', 'right': 'left', '1': 'one'}
>>> text = 'Up down left Right forward 1 time'
>>> translate(myDict, text)
'down up right left forward one time'
"""
words = txt.lower().split()
for i in range(len(words)):
if words[i] in translation:
words[i] = translation[words[i]]
return ' '.join(words)
def onlyTwo(x, y, z):
"""
>>> onlyTwo(1, 2, 3)
13
>>> onlyTwo(3, 3, 2)
18
>>> onlyTwo(5, 5, 5)
50
"""
m1 = max(x, y, z)
m2 = (x + y + z) - min(x, y, z) - m1
return m1 * m1 + m2 * m2
def largeFactor(num):
"""
>>> largeFactor(15)
5
>>> largeFactor(24)
12
>>> largeFactor(7)
1
"""
n = num - 1
while n >= 1:
if num % n == 0:
return n
n -= 1
def hailstone(num):
"""
>>> hailstone(5)
[5, 16, 8, 4, 2, 1]
>>> hailstone(6)
[6, 3, 10, 5, 16, 8, 4, 2, 1]
"""
# --- YOU CODE STARTS HERE
result = []
while num > 1:
result.append(num)
if num % 2 == 0:
num = num // 2
else:
num = 3 * num + 1
result.append(num)
return result