Python Codes

pdf

School

Harvard University *

*We aren’t endorsed by this school

Course

228

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

5

Uploaded by LieutenantDragon138

Report
Rock paper scissors # Rock paper scissor game # 10/25/2021 import random player_score = 0 computer_score = 0 def rockPaperScissors(): global player_score global computer_score user_action = input("Enter a choice (rock[r], paper[p], scissors[s]): ") possible_actions = ["r", "p", "s"] computer_action = random.choice(possible_actions) if user_action not in possible_actions: print("Invalid input, try again") else: print(f"\nUser: {user_action}\nComputer: {computer_action}\n") if user_action == computer_action: print(f"Both players selected: {user_action}\nIt's a tie!") elif user_action == "r": if computer_action == "s": player_score += 1 print("Rock smashes scissors! You win!") else: computer_score += 1 print("Paper covers rock! You lose.") elif user_action == "p": if computer_action == "r": player_score += 1 print("Paper covers rock! You win!") else: computer_score += 1 print("Scissors cuts paper! You lose.") elif user_action == "s": if computer_action == "p": player_score += 1 print("Scissors cuts paper! You win!") else: computer_score += 1 print("Rock smashes scissors! You lose.") print ("\nYour score:", str(player_score)+ ", Computer score:", str(computer_score))
while True: rockPaperScissors() Prime numbers displayer # Prime number displayer def is_prime(integer): if (integer==1): return False elif (integer==2): return True else: for x in range(2,integer): if (integer % x == 0): return False return True for i in range(1,101): if is_prime(i): if i==97: print(i) else: print(i) Prime number checker # Prime number indicator def is_prime(integer): if (integer==1): return False elif (integer==2): return True else: for x in range(2,integer): if (integer % x == 0): return False return True Monthly tax sales
# Monthly tax calculator def tax(sales): stateTax=(sales*.05) countyTax=(sales*.025) totalSales=(sales-(stateTax+countyTax)) print(f"State sales tax is: ${stateTax:.2f}") print(f"County sales tax is: ${countyTax:.2f}") print(f"Your total monthly sales after tax: ${totalSales:.2f}") Merge sort algorithm # Merge sort algorithm def mergeSort(myList): if len(myList) > 1: mid = len(myList) // 2 left = myList[:mid] right = myList[mid:] # Recursive call on each half mergeSort(left) mergeSort(right) # Two iterators for traversing the two halves i = j = k = 0 # K is Iterator for the main list while i < len(left) and j < len(right): if left[i] <= right[j]: # The value from the left half has been used myList[k] = left[i] # Move the iterator forward i += 1 else: myList[k] = right[j] j += 1 # Move to the next slot k += 1 # For all the remaining values while i < len(left): myList[k] = left[i] i += 1 k += 1 while j < len(right): myList[k]=right[j]
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
j += 1 k += 1 myList = [99,50,47,69,22,100,1,2,5,18,19,24] mergeSort(myList) print(myList) Caesar cipher algorithm LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ' LETTERS = LETTERS.lower() # Encrypt def encrypt(word,shift): encrypted='' for i in range(len(word)): char=word[i] if (char.isupper()): encrypted+=chr((ord(char)+shift-65)% 26+65) else: encrypted+=chr((ord(char)+shift-97)%26 +97) return encrypted # Decrypt def decrypt(message, key): decrypted = '' for i in range(len(message)): char=message[i] if (char.isupper()): decrypted+=chr((ord(char)-key-65)% 26+65) else: decrypted+=chr((ord(char)-key-97)%26 +97) return decrypted # Main def main(): message = str(input('Enter your message: ')) key = int(input('Enter you key [1 - 26]: ')) choice = input('Encrypt or Decrypt? [E/D]: ') if choice.lower().startswith('e'): print(encrypt(message, key)) else: print(decrypt(message, key))
if __name__ == '__main__': main() SIG Coding Challenge 2023 def blockStorageRewrites(blockCount, writes, threshold): rewrite_counts = [0] * blockCount # Initialize rewrite counts for each block result = [] for write in writes: first_block, last_block = write for block in range(first_block, last_block + 1): rewrite_counts[block] += 1 start_block = None for block in range(blockCount): if rewrite_counts[block] >= threshold: if start_block is None: start_block = block elif start_block is not None: result.append([start_block, block - 1]) start_block = None if start_block is not None: result.append([start_block, blockCount - 1]) return result # Example usage: blockCount = 10 writes = [[0, 4], [3, 5], [2, 6]] threshold = 2 result = blockStorageRewrites(blockCount, writes, threshold) print(result) # Output: [[2, 5]]