hw1

py

School

Pennsylvania State University *

*We aren’t endorsed by this school

Course

132

Subject

Mechanical Engineering

Date

Dec 6, 2023

Type

py

Pages

3

Uploaded by ConstableValorBear40

Report
import math def rectangle(perimeter, area): for w in range(1, perimeter // 2 + 1): h = (perimeter - 2 * w) // 2 if w * h == area and w > 0 and h > 0 and float(w).is_integer() and float(h).is_integer(): return max(w, h) return False def to_decimal(oct_num): result = 0 multiplier = 1 while oct_num > 0: digit = oct_num % 10 # Get the rightmost octal digit result += digit * multiplier multiplier *= 8 oct_num //= 10 # Remove the processed digit return result def has_hoagie(num): num = abs(num) # Convert negative numbers to positive if num < 100: return False # Numbers with less than three digits cannot have a hoagie while num >= 100: third_digit = num % 10 num //= 10 second_digit = num % 10 num //= 10 first_digit = num % 10 if first_digit == second_digit or second_digit == third_digit: return True # Hoagie found return False # No hoagie found def is_identical(num_1, num_2): def remove_repeated_digits(num): result = 0 prev_digit = None while num > 0: digit = num % 10 num //= 10 if digit != prev_digit: result = result * 10 + digit prev_digit = digit return result num_1_cleaned = remove_repeated_digits(num_1)
num_2_cleaned = remove_repeated_digits(num_2) return num_1_cleaned == num_2_cleaned def hailstone(num): sequence = [] while num != 1: sequence.append(num) if num % 2 == 0: num //= 2 else: num = 3 * num + 1 sequence.append(1) return sequence def overloaded_add(d, key, value): if key in d: # If the existing value is a list, append the new value to it if type(d[key]) == list: d[key].append(value) else: # If it's not a list, convert it into a list and add both values d[key] = [d[key], value] else: # If the key doesn't exist, simply add it with the initial value d[key] = value def by_department(d): result = {} # Initialize the result dictionary for emp_id, info in d.items(): department = info['department'] name = info['name'] position = info['position'] # Create a dictionary for the employee with the desired format emp_info = {'emp_id': emp_id, 'name': name, 'position': position} # Check if the department key exists in the result dictionary if department in result: result[department].append(emp_info) else: # If the department doesn't exist, create a new list with the employee info result[department] = [emp_info] return result def successors(file): words = [] words.append(".") dictionary = {} # Initialize the dictionary outside the loop with open(file, 'r') as f: for line in f: x = "" for i in range(len(line)): if line[i].isalnum():
x += line[i] else: if len(x) > 0: words.append(x) x = "" if line[i] != " " and line[i] != "\n": words.append(line[i]) for i in range(len(words) - 1): if words[i] in dictionary: if words[i + 1] not in dictionary[words[i]]: dictionary[words[i]].append(words[i + 1]) else: dictionary[words[i]] = [words[i + 1]] return dictionary def addToTrie(trie, word): current = trie # Start at the root of the trie for char in word: # Check if the character is already a key in the current dictionary if char not in current: current[char] = {} # Create a new dictionary for the character # Update the current variable to point to the new dictionary current = current[char] # Set 'word' to True to indicate that a complete word has been added current['word'] = True
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