From 631629666879283751e847c6f74a999448a525bd Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 5 Mar 2025 13:15:26 +0200 Subject: [PATCH 1/2] Add 218014739 Assignment 1.py --- 218014739 Assignment 1.py | 94 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 218014739 Assignment 1.py diff --git a/218014739 Assignment 1.py b/218014739 Assignment 1.py new file mode 100644 index 0000000..bd872ac --- /dev/null +++ b/218014739 Assignment 1.py @@ -0,0 +1,94 @@ +################################ +#### EXERCISE 1 30 MARKS ##### +################################ + + +# Time to review all the basic data types we learned! This should be a +# relatively straight-forward and quick assignment. + +################ +## Problem 1 - 10 Points ## +################ + +# Given the string: +s = 'fullstackslp' + +# Use indexing to print out the following: +# 'f' +print(s[0]) + +# 'p' +print(s[-1]) + +# 'stack' +print(s[4:9]) + +# 'slp' +print(s[9:]) + +# 'cks' +print(s[7:10]) + +# Bonus: Use indexing to reverse the string +print(s[::-1]) + + + +################ +## Problem 2 - LISTS - 5 Marks ## +################ + +# Given this nested list: +l = [3,7,[5,[1,4,'hello']]] +# Reassign "hello" to be "goodbye" + +l[2][1][2] = "goodbye" + +print(l) + + + +################ +## Problem 3 - DICTIONARIES - 6 Marks ## +################ + +# Using keys and indexing, grab the 'hello' from the following dictionarties: + +d1 = {'simple_key':'hello'} +print(d1['simple_key']) + +d2 = {'k1':{'k2':'hello'}} +print(d2['k1']['k2']) + +d3 = {'k1':[{'nest-key':['this is deep',['hello']]}]} +d3New = d3['k1'][0]['nest-key'][1][0] + +print(d3New) + + +############### +## Problem 4 - SETS - 4 Marks ## +############### + +# Use a set to find the unique values of the list below: +mylist = [1,1,1,1,1,2,2,2,2,3,3,3,3] + +newlist = set(mylist) + +print(newlist) + + +############### +## Problem 5 - FORMATTING - 5 Marks ## +############### + + +# You are given two variables: +age = 45 +name = "Kyle" + + +# Use print formatting to print the following string: +s = "Hello my dog's name is {} and he looks {} years old".format(name,age) + +print(s) \ No newline at end of file From f4b66f5e195b962e1db1e67cfcab1bb63fcdd892 Mon Sep 17 00:00:00 2001 From: MatlogaC <135592774+MatlogaC@users.noreply.github.com> Date: Wed, 12 Mar 2025 17:17:20 +0200 Subject: [PATCH 2/2] Assignment 2 added --- 218014739_Assignment2.py | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 218014739_Assignment2.py diff --git a/218014739_Assignment2.py b/218014739_Assignment2.py new file mode 100644 index 0000000..27dd8af --- /dev/null +++ b/218014739_Assignment2.py @@ -0,0 +1,52 @@ +# Problem 1: Check if a sequence of numbers appears in the list +#Check if a sequence of numbers appears in the list + +def list_check(num): + return 3 in num +print(list_check([0, 2, 3])) + + +#Problem 2: Extract every second character +def string_bit(str): + return str[::2] +print(string_bit("heeololeo")) + + +#Problem 3: Check if one string is the end of the other +def end_other(a, b): + a, b = a.lower(), b.lower() + return a.endswith(b) or b.endswith(a) +print(end_other("Charles", "char")) + + +#Problem 4: Double characters +def double_char(str): + ''' + Return a string where for every char in the original, there are two chars. + ''' + return ''.join([char * 2 for char in str]) + +print(double_char("Hope")) + +#Problem 5 + +def fix_teen(n: int) -> int: + """ + Takes an integer and returns 0 if it's a 'teen' (13-19) except for 15 and 16. + """ + return 0 if 13 <= n <= 19 and n not in (15, 16) else n + +def no_teen_sum(a: int, b: int, c: int) -> int: + """ + Returns the sum of three numbers, treating most teen numbers (13-19) as 0, + except for 15 and 16, which are counted normally. + """ + return fix_teen(a) + fix_teen(b) + fix_teen(c) + +print(no_teen_sum(1, 15, 19)) + +#Problem 6: Count even numbers + +def count_evens(nums): + return sum(1 for num in nums if num % 2 == 0) +print(count_evens([2, 1, 2, 6, 4])) \ No newline at end of file