Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/wXQiiW8O)
# Lab3
22 changes: 22 additions & 0 deletions Task_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
more = [x + 1 for x in [1,2,3,4]] # List, in order, the values that x takes at each step.
print() # more=[2,3,4,5]


def square(n:int) -> int:
return n * n # Call 1: n=1, returns 1. Call 2: n=2, returns 4. Call 3: n=3, returns 9. Call 4: n=4, returns 16
squares = [square(x) for x in [1,2,3,4]] # Squares is a list of the return values for each call of the function
print()


def check(n:int) -> bool:
return n > 2 # Call 1: n=0, returns False. Call 2: n=1, returns False. Call 3: n=2, returns False. Call 4: n=3, returns True. Call 5: n=4, returns True
answer = [x for x in range(5) if check(x)] # answer=[3,4]
print()


def inc(m:int) -> int:
return m + 1 # Call 1: m=3, returns 4. Call 2: m=4, returns 5.
def check(n:int) -> bool:
return n > 2 # Call 1: n=0, returns False. Call 2: n=1, returns False. Call 3: n=2, returns False. Call 4: n=3, returns True. Call 5: n=4, returns True
answer = [inc(x) for x in range(5) if check(x)] # answer=[4,5]
print()
20 changes: 20 additions & 0 deletions Task_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def tally(nums:list[int]) -> int:
total = 0
for num in nums:
total = total + num # 1: num=4, total=4. 2: num=9, total=13. 3: num=2, total=15. 4: num=1, total=16
return total
result = tally([4, 9, 2, 1])

def copy(nums:list[int]) -> list[int]:
new_list = []
for idx in range(len(nums)):
new_list.append(nums[idx]) # 1: idx=0, new_list=[4]. 2: idx=1, new_list=[4,9]. 3: idx=2, new_list=[4,9,2]. 4: idx=3, new_list=[4,9,2,1]
return new_list # This loop indexes the input list and makes a new list with all the same numbers in the same place
result = copy([4, 9, 2, 1])

def increment_all(nums:list[int]) -> list[int]:
new_list = []
for value in nums:
new_list.append(value + 1) # 1: value=4, new_list=[5]. 2: value=9, new_list=[5,10]. 3: value=2, new_list=[5,10,3]. 4: value=1, new_list=[5,10,3,2]
return new_list
result = increment_all([4, 9, 2, 1])
2 changes: 1 addition & 1 deletion functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# Input: a number to be doubled
# Result: a number
def double(n:int) -> int:
result = n * n
result = 2 * n
return result
3 changes: 3 additions & 0 deletions testcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ def test_double_two(self):

if __name__ == '__main__':
unittest.main()

# test_double_one giving the right answer doesn't imply that the double function is correct.
# For the value of 2, 2*2=2**2, so it outputs the expected value even if the code is fundamentally incorrect.