Answer To: 109 Python Problems for CCPS 109 CCPS 109 Computer Science I Labs Ilkka Kokkarinen Chang School of...
Robert answered on Jun 20 2022
40 PYTHON ANSWERS
1. Ryerson letter
def ryerson_letter_grade(pct):
if pct < 50:
return 'F'
elif pct > 89:
return 'A+'
elif pct > 84:
return 'A'
elif pct > 79:
return 'A-'
tens = pct // 10
ones = pct % 10
if ones < 3:
adjust = "-"
elif ones > 6:
adjust = "+"
else:
adjust = ""
return "DCB"[tens - 5] + adjust
2. Ascending list
def is_ascending(items):
j = 1
while j < len(items):
if (items[j] <= items[j - 1]):
return False
j + = 1
return True
3. The card that wins the trick
def winning_card(cards, trump=None):
ranks = [
"ace",
"king",
"queen",
"jack",
"ten",
"nine",
"eight",
"seven",
"six",
"five",
"four",
"three",
"two",
]
if trump is None:
suit_first = cards[0][1]
else:
suit_first = cards[0][1]
for card in cards:
if trump in card:
suit_first = trump
break
highest = 0
card_h = cards[0]
for card in cards:
if suit_first in card:
if card[1] == suit_first:
highest = ranks.index(card[0])
card_h = card
break
for card in cards:
if card[1] == suit_first:
if highest > ranks.index(card[0]):
highest = ranks.index(card[0])
card_h = card
return card_h
4. Sevens rule, zeros drool
def seven_zero(n):
def generator():
num = str(n)
for d in range(len(num)):
if (d + 1) < len(num):
nn = int(num[d]) * (10 ** (len(num) - (d + 1)))
else:
nn = int(num[d])
for k in range(1, nn + 1):
yield (k * '7') + ((nn - k) * '0')
result = ''
for g in generator():
result += g + '\n'
return result
print(seven_zero(17))
5. Bulgarian solitaire
def bulgarian_solitaire (piles, k):
state = set (range (1, k + 1))
n = 0
while True:
if (set (piles) -{0})== state:
break
else:
n += 1
count = len ([pile for pile in piles if pile > 0])
piles =[pile - 1 for pile in piles if pile > 0]
piles.append (count)
return n
#print(bulgarian_solitaire([8,3,3,1], 5))
#print(bulgarian_solitaire([10,10,10,10,10,5], 10))
6. Collecting numbers
def collect_numbers(perm):
#print(perm)
inv = [0] * len(perm)
for j, i in enumerate(perm):
inv[i] = j
count = 1
if len(perm) == 1:
return count
else:
for i in range(0, len(inv)-1):
if inv[i+1] < inv[i]:
count += 1
return count
#print(collect_numbers([0, 1, 2, 3, 4, 5])
#print(collect_numbers([2, 0, 1])
#print(collect_numbers([0, 4, 3, 5, 2, 1])
#print(collect_numbers([8, 6, 9, 5, 4, 11, 2, 0, 3, 10, 12, 1, 7])
#print(collect_numbers(list(range(10**6, -1, -1))))
7. Reverse ascending sublists
def reverse_ascending_sublists(items):
if items=[]:
return []
result = [[items[0]]]
for i in range(1, len(items)):
if items[i-1] result[-1].append(items[i])
else:
result.append([items[i]])
reverse_result = [i[-1] for (i, j) in enumerate(result, 1)]
flatten lambda *n: (e for a in n for e in (flatten(a) if isinstance(a, (tuple, list)) else (a,)))
return list(flatten(reverse_result))
8. Brangelin-o-matic for the people
def brangelina(first, second):
vowels = "aeiou"
n2 = second[sorted([(second+vowels).index(vowel) for vowel in vowels])[0]:]
s = []
for i,letter in enumerate(first):
s.append([letter."][il=0 and first[i-1] in vowels])
onlyv = [letter for letter in s if letter in vowels]
if len(onlyv)==1: # 1 vowel group
n1 first[ first.index(onlyv[0])]
ship="join(n1+n2)
return str(ship)
else: #> 1 vowel groups
n1 = first[:first.index(onlyv[-2])]
ship="" join(n1+n2)
return str(ship)
9. Cyclops numbers
def is_cyclops(n):
str_converted = str(n)
if(len(str_converted) % 2 == 0):
return False
else:
middle_index = len(str_converted) // 2
if str_converted[middle_index] != "0": return False
if str_converted.count("0") > 1: return False
return True
10. Even the odds
def only_odd_digits(n):
return set(str(n)) - set('13579') == set()
11. Count Dominators
def count_dominators(items):
dominators = 0
for index,item in enumerate(items):
dominator = True
for right_item in items[index + 1:]:
if item <= right_item:
dominator = False
break
if dominator:
dominators = dominators + 1
return dominators
12. Try a Spatula
def pancake_scramble(text):
for i in range(1, len(text)):
text = text[i::-1] + text[(i + 1) ::]
return text
13. Taxi Zum Zum
def taxi_zum_zum(moves):
directions = ['N','E','S','W']
dir = 0
x = 0
y = 0
for i in moves:
if i == 'L':
dir -= 1
if dir < 0:
dir = 3
elif i== 'R':
dir += 1
if dir > 3:
dir = 0
elif i == 'F':
if directions[dir]=='N':
y += 1
elif directions[dir]=='S':
y -= 1
elif directions[dir]=='E':
x += 1
elif directions[dir]=='W':
x -= 1
return (x,y)
14. Chirality
def is_left_handed(pips):
...