From f3fc46c2b263e2ca925607cb731375d5b8e87572 Mon Sep 17 00:00:00 2001 From: Xinshuang Jin Date: Mon, 14 Apr 2025 18:23:35 -0400 Subject: [PATCH] modify rotate_list and skyline --- rotate_list.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++- skyline.py | 38 ++++++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/rotate_list.py b/rotate_list.py index 383245a..c56f8e2 100644 --- a/rotate_list.py +++ b/rotate_list.py @@ -1,4 +1,64 @@ # Add your clarifying questions here +# Will the value of shift_by always be positive? + # Yes, always a positive shift by +# Is the list always non-empty? + # An empty list will just return an empty +# Will the shift alway be to the right? + # Yes, we wil never shift to the left +# What if the shift_by is larger than our length? + # Shift by will be alwasy be less than the length + # let group decide +# Are we allowed to use built ins(pop, insert)? +# if there is only one element in the list, return the list? +# return None, shift out of bounds + +# input : [1, 2, 3] , 2 +# output: [2, 3, 1]\ +#asssert rotate_list([1, 2, 3], 2) = [2, 3, 1] + +# input: ['a', 'b', 'c', 'd'] , 1 +# output": ['d','a','b', 'c'] + + +# input = [9], 5233 +# output: None, or shift out of boundsĀ  + +# Method 1 def rotate_list(list, shift_by): - pass \ No newline at end of file + if not list: + return [] + if len(list) < shift_by: + return f"shift_by {shift_by} is out of bounds" + if len(list) == 1: + return list + + updated_list = [None] * len(list) # update_list = [None, None, None] + + for i in range(len(list)): + if i + shift_by > len(list) - 1: + updated_list[i+shift_by-len(list)] = list[i] + continue + updated_list[i+shift_by] = list[i] + + return updated_list + +result = rotate_list(['a', 'b', 'c', 'd'] , 1) +print(result) + +# Method 2 hard code, not add a new list! +def rotate_list(list, shift_by): + if not list: + return [] + if len(list) < shift_by: + return f"shift_by {shift_by} is out of bounds" + if len(list) == 1: + return list + + return list[(len(list) - shift_by): len(list)] + list[0: (len(list) - shift_by)] + + + # This also works + # return list[(len(list) - shift_by): ] + list[: (len(list) - shift_by)] +result = rotate_list([1, 2, 3] , 2) +print(result) diff --git a/skyline.py b/skyline.py index 20857c3..ceb2c04 100644 --- a/skyline.py +++ b/skyline.py @@ -1,4 +1,40 @@ # Add your clarifying questions here +# Find smallest in the list, anythin above that is you could seens, order there is ascending order with no repeats + +# Only one elemetn in the list? return None +# if the list if empty? return None +# if the elemenets are all the same? return None +# if the elements are not numbers? assume they are all numbers + + +# Given a list [-1, 1, 3, 7, 7, 3] determine which values could be "seen." +# The output should be: [1,3,7] + def skyline(building_list): - pass \ No newline at end of file + if not building_list: + return None + if len(building_list) == 1: + return None + + # bottom_floor = min(building_list, key=lambda element: element) + bottom_floor = min(building_list) + top_floor = max(building_list) + + seen_list = [] + previous_floor = float('-inf') + + for floor in building_list: + # If it reaches the top floor, nothing else could be seen, exit the loop + if floor == top_floor: + seen_list.append(floor) + break + if (floor > bottom_floor) and (floor > previous_floor): + seen_list.append(floor) + previous_floor = floor + + return seen_list + + +result = skyline([-1, 1, 3, 7, 7, 3]) +print(result) \ No newline at end of file