Advertisement
Aikiro42

LabExer7

Nov 18th, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. from math import floor
  2. from itertools import starmap, cycle
  3.  
  4. # taxi_fare
  5. def compute_fare(average_speed, travel_time):
  6.     average_speed /= 60
  7.     fare = 40
  8.     fare += 13.50*floor(average_speed*travel_time)
  9.     fare += 2*floor(travel_time)
  10.     return fare
  11.  
  12.  
  13. # GWA_calculator
  14. def calculate_GWA(courses):
  15.     total_units = gwa = 0
  16.     for course in courses:
  17.         units = course[1]
  18.         grade = course[2]
  19.         if '(' in units and ')' in units:
  20.             continue
  21.         if grade in ['P','F','INC','']:
  22.             continue
  23.         total_units += float(units)
  24.         gwa += eval(units + '*' + grade)
  25.     if total_units == 0 or gwa == 0:
  26.         return ''
  27.     gwa /= total_units
  28.     return str(round(gwa,2))
  29.  
  30.  
  31. # vigenere_cipher
  32. def vigenere_encrypt(message, key):
  33.     join_message = ''.join(message.split(' '))
  34.     cipher = ''.join(starmap(lambda x,y:chr((ord(x) + ord(y) - 130)%26 + 65), zip(join_message, cycle(key))))
  35.     x = 0
  36.     spaced_cipher = ''
  37.     for i in message:
  38.         if i == ' ':
  39.             spaced_cipher += ' '
  40.             continue
  41.         spaced_cipher += cipher[x]
  42.         x += 1
  43.     print(spaced_cipher)
  44.     return spaced_cipher
  45.  
  46. # vigenere_cipher, no starmap nor cycle
  47. def vigenere_encrypt_conventional(message, key):
  48.     key_char = 0
  49.     cipher = ''
  50.     for letter in message:
  51.         if key_char == len(key):
  52.             key_char = 0
  53.         if letter == ' ':
  54.             cipher += ' '
  55.         else:
  56.             cipher += chr((ord(letter) + ord(key[key_char]) - 130)%26 + 65)
  57.             key_char += 1
  58.     return cipher
  59.  
  60. # keyed sorting
  61. f1 = lambda x: (int(str(x).split('.')[1][0]), x)
  62. f2 = lambda x: (int(str(x**3)[-2:]), x)
  63. f3 = lambda x: (len(x), x)
  64. f4 = lambda x: (sum(map(ord, x)), x)
  65. f5 = lambda x: (x[1], x[0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement