adding as-is from uni onedrive
This commit is contained in:
commit
107684f809
12
Exercise1.py
Normal file
12
Exercise1.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
unsorted_array = [6,2,9,0,2,5,3]
|
||||||
|
sorted_array = []
|
||||||
|
|
||||||
|
while unsorted_array:
|
||||||
|
minimum_value = unsorted_array[0]
|
||||||
|
for x in unsorted_array:
|
||||||
|
if x < minimum_value:
|
||||||
|
minimum_value = x
|
||||||
|
sorted_array.append(minimum_value)
|
||||||
|
unsorted_array.remove(minimum_value)
|
||||||
|
print sorted_array[len(sorted_array)-1]
|
||||||
|
#print sorted_array
|
35
Exercise1Try1.py
Normal file
35
Exercise1Try1.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
numbers = [6,2,9,0,2,5,3] #list of input numbers
|
||||||
|
print numbers #test
|
||||||
|
#sorted = [numbers[0]]
|
||||||
|
sorted = [] #list of sorted numbers
|
||||||
|
#print sorted #test
|
||||||
|
counter = 1 #counter to be used as reference for input numbers list
|
||||||
|
sorted_counter = counter-1 #coutner to be used as reference for positioning in sorted list
|
||||||
|
#print sorted_counter #test
|
||||||
|
#print len(numbers) #test
|
||||||
|
#while counter <= len(numbers):
|
||||||
|
# if numbers[counter] < sorted[sorted_counter]:
|
||||||
|
# sorted.insert(sorted_counter, numbers[counter])
|
||||||
|
# counter += 1
|
||||||
|
# print sorted
|
||||||
|
# print counter
|
||||||
|
# else:
|
||||||
|
# counter += 1
|
||||||
|
|
||||||
|
for x in numbers:
|
||||||
|
print x
|
||||||
|
if counter == 1:
|
||||||
|
sorted.append(x)
|
||||||
|
counter += 1
|
||||||
|
else:
|
||||||
|
for y in sorted:
|
||||||
|
if x < y:
|
||||||
|
print "yes"
|
||||||
|
break
|
||||||
|
# if numbers[x] < sorted[sorted_counter]:
|
||||||
|
# sorted.insert(sorted_counter, numbers[counter])
|
||||||
|
# counter += 1
|
||||||
|
# print sorted
|
||||||
|
# print counter
|
||||||
|
# else:
|
||||||
|
# counter += 1
|
4
README.md
Normal file
4
README.md
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
Python Labs
|
||||||
|
==============
|
||||||
|
|
||||||
|
Lab files and exercises for first year python module.
|
9
booleaninto.py
Normal file
9
booleaninto.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
x = 2
|
||||||
|
print x == 2 #true
|
||||||
|
print x is 2 # true
|
||||||
|
print x == 3 #false
|
||||||
|
|
||||||
|
str1 = "hello"
|
||||||
|
str2 = str1
|
||||||
|
print str1 is str2 #true
|
||||||
|
print str1 == str2 #true
|
6
date.py
Normal file
6
date.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
from datetime import date
|
||||||
|
|
||||||
|
d0 = date(2008, 8, 18)
|
||||||
|
d1 = date(2008, 9, 26)
|
||||||
|
delta = d0 - d1
|
||||||
|
print delta.days
|
26
dictionaryintro.py
Normal file
26
dictionaryintro.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
phonebook = {} #empty dictionary, {} for dictionary [] for list
|
||||||
|
phonebook["Jack"] = "1"
|
||||||
|
phonebook["Nikos"] = "2"
|
||||||
|
phonebook ["Fiona"] = "3"
|
||||||
|
|
||||||
|
print phonebook["Jack"] #prints phone number of jack
|
||||||
|
|
||||||
|
phonebookother = { #other way to open dictionary"
|
||||||
|
"jack": "1",
|
||||||
|
"nikos": "2",
|
||||||
|
"fiona" : "3"
|
||||||
|
}
|
||||||
|
|
||||||
|
phonebook["John"] = "4" #adds element
|
||||||
|
|
||||||
|
for key, value in phonebook.iteritems():
|
||||||
|
print key + "'s phone number is " + str(value)
|
||||||
|
|
||||||
|
phonebook["Nikos"] = "7" #change number
|
||||||
|
|
||||||
|
print phonebook["Nikos"] #reprint
|
||||||
|
|
||||||
|
del phonebook["Fiona"] #deletes number
|
||||||
|
|
||||||
|
for key, value in phonebook.iteritems():
|
||||||
|
print key + "'s phone number is " + str(value)
|
31
exercise3.py
Normal file
31
exercise3.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
try:
|
||||||
|
day_1 = eval(raw_input("Enter Day 1: "))
|
||||||
|
except NameError:
|
||||||
|
print "fucked it"
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
month_1 = eval(raw_input("Enter Month 1: "))
|
||||||
|
except NameError:
|
||||||
|
print "fucked it"
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
year_1 = eval(raw_input("Enter Year 1: "))
|
||||||
|
except NameError:
|
||||||
|
print "fucked it"
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
day_2 = eval(raw_input("Enter Day 2: "))
|
||||||
|
except NameError:
|
||||||
|
print "fucked it"
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
month_2 = eval(raw_input("Enter Month 2: "))
|
||||||
|
except NameError:
|
||||||
|
print "fucked it"
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
year_2 = eval(raw_input("Enter Year 2: "))
|
||||||
|
except NameError:
|
||||||
|
print "fucked it"
|
||||||
|
pass
|
||||||
|
print day_1
|
41
exercise32.py
Normal file
41
exercise32.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
day_1 = eval(raw_input("Enter Day 1: "))
|
||||||
|
month_1 = eval(raw_input("Enter Month 1: "))
|
||||||
|
year_1 = eval(raw_input("Enter Year 1: "))
|
||||||
|
day_2 = eval(raw_input("Enter Day 2: "))
|
||||||
|
month_2 = eval(raw_input("Enter Month 2: "))
|
||||||
|
year_2 = eval(raw_input("Enter Year 2: "))
|
||||||
|
|
||||||
|
month_length = [0, 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
||||||
|
|
||||||
|
month_1_math = month_1
|
||||||
|
day_sum = 0
|
||||||
|
while month_1_math != 0:
|
||||||
|
day_sum += month_length[month_1_math]
|
||||||
|
month_1_math -= 1
|
||||||
|
|
||||||
|
month_2_math = 11 - month_2
|
||||||
|
day_sum_2 = 0
|
||||||
|
while month_2_math != 0:
|
||||||
|
day_sum_2 += month_length[month_2_math]
|
||||||
|
month_2_math -= 1
|
||||||
|
|
||||||
|
day_2_difference = month_length[month_2] - day_2
|
||||||
|
|
||||||
|
year_dif = ((year_1 - year_2 - 1)*365)
|
||||||
|
|
||||||
|
if year_dif < 0:
|
||||||
|
year_dif = 0
|
||||||
|
|
||||||
|
total = year_dif + day_sum + day_1 + day_sum_2 + day_2_difference
|
||||||
|
print total
|
||||||
|
#delta_day = day_2 - day_1
|
||||||
|
#delta_month = month_2 - month_1
|
||||||
|
#delta_year = year_2 - year_1 - 1
|
||||||
|
|
||||||
|
#
|
||||||
|
#if day_2 < day_1:
|
||||||
|
# delta_month -= 1
|
||||||
|
#
|
||||||
|
#if month_2 < month_2:
|
||||||
|
# delta_year -= 1
|
||||||
|
|
11
exercise_1.py
Normal file
11
exercise_1.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
unsorted_array = [6,2,9,0,2,5,3] #array of values given by exercise
|
||||||
|
sorted_array = [] #empty array that will be populated by sorted values
|
||||||
|
|
||||||
|
while unsorted_array: #open while loop, active while the unsorted array still has values in it
|
||||||
|
minimum_value = unsorted_array[0] #takes first value in array and sets it as "minimum value"
|
||||||
|
for x in unsorted_array:
|
||||||
|
if x < minimum_value: #for loops check "minimum value" against each value in array
|
||||||
|
minimum_value = x #and sets it as new "minimum value" if a value is found that is smaller
|
||||||
|
sorted_array.append(minimum_value) #adds smallest value from unsorted array to the first value of the sorted array
|
||||||
|
print minimum_value #prints smallest value
|
||||||
|
unsorted_array.remove(minimum_value)#removes smallest value from unsorted array so it can not be picked and added again
|
26
exercise_2.py
Normal file
26
exercise_2.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
rows = eval(raw_input("Enter Row Number: ")) #user defines number of columns
|
||||||
|
columns = eval(raw_input("Enter Column Number: ")) #user defines number of columns
|
||||||
|
|
||||||
|
matrix = [] #define empty matrix
|
||||||
|
|
||||||
|
for i in range(0, rows): #populates matrix with user elements
|
||||||
|
newrow = []
|
||||||
|
for j in range(0, columns): #defines a row of elements added by user and appends it to matrix
|
||||||
|
num = int(raw_input("Enter Matrix Element: "))
|
||||||
|
newrow.append(num)
|
||||||
|
matrix.append(newrow)
|
||||||
|
|
||||||
|
for i in matrix: #print first matrix
|
||||||
|
print i
|
||||||
|
|
||||||
|
transposed_matrix = [] #defines new matrix which will become transpose of previous matrix
|
||||||
|
|
||||||
|
for i in range(0, columns): #same function as before to populate new matrix, defining number of rows as
|
||||||
|
newrow = [] #previous matrix had columns and vise versa
|
||||||
|
for j in range(0, rows):
|
||||||
|
num = matrix[j][i] #takes each element from previous matrix rather than user input as before
|
||||||
|
newrow.append(num)
|
||||||
|
transposed_matrix.append(newrow)
|
||||||
|
|
||||||
|
for i in transposed_matrix:
|
||||||
|
print i #print transpose matrix
|
24
exercise_3.py
Normal file
24
exercise_3.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
print "Function takes Date 1 and subtracts Date 2, enter string of integers separated by spaces"
|
||||||
|
|
||||||
|
date_1 = raw_input("Enter date 1 in format dd mm yyyy: ") #takes dates as string of integers
|
||||||
|
date_2 = raw_input("Enter date 2 in format dd mm yyyy: ")
|
||||||
|
|
||||||
|
date_1 = [int(i) for i in date_1.split() if i.isdigit()] #creates array of integers extracted from string input
|
||||||
|
date_2 = [int(j) for j in date_2.split() if j.isdigit()]
|
||||||
|
|
||||||
|
month_length = [0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]
|
||||||
|
#month_length is array of months denoted by cumulative days up to month before month entered hence 0,0 at the start
|
||||||
|
#ie entering 1 as january will return 0 as no complete months have passed
|
||||||
|
#entering 3 for march will return 59 representing january's and february's complete days
|
||||||
|
|
||||||
|
def date_to_days(day, month, year): #function defined to take date and turn it into days
|
||||||
|
year *= 365 #takes total years inputed and multiplies it by 365
|
||||||
|
month = month_length[month] #returns days in month from array previously explained
|
||||||
|
return day + month + year #returns total days as sum of previous calculations
|
||||||
|
|
||||||
|
date_1_days = date_to_days(date_1[0], date_1[1], date_1[2]) #calls function for first date
|
||||||
|
date_2_days = date_to_days(date_2[0], date_2[1], date_2[2]) #calls function for second date
|
||||||
|
|
||||||
|
#print date_1_days #prints everything
|
||||||
|
#print date_2_days
|
||||||
|
print abs(date_1_days-date_2_days) #takes absolute value so days is always positive
|
9
functionintro.py
Normal file
9
functionintro.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
def my_print(name):
|
||||||
|
print "hello " + name + "!."
|
||||||
|
|
||||||
|
my_print("Andy")
|
||||||
|
|
||||||
|
def my_sum(x,y):
|
||||||
|
return x+y
|
||||||
|
|
||||||
|
print my_sum(4,5)
|
1
helloworld.py
Normal file
1
helloworld.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
print "Hello World" #comment
|
9
inputintro.py
Normal file
9
inputintro.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
mystr = raw_input("enter a string: ")
|
||||||
|
print mystr
|
||||||
|
myint = eval(raw_input("enter a number: "))
|
||||||
|
print str(myint)
|
||||||
|
mymatrix = raw_input("enter characters separated by commas: ")
|
||||||
|
print mymatrix.split(",") #split a string into a list of elements where the delimiter is ,
|
||||||
|
|
||||||
|
for x in mymatrix:
|
||||||
|
print x
|
11
listappend.py
Normal file
11
listappend.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
lst = [] # empty list
|
||||||
|
lst.append(1)
|
||||||
|
lst.append(2)
|
||||||
|
lst.append(3)
|
||||||
|
print(lst[0])
|
||||||
|
print(lst[2])
|
||||||
|
|
||||||
|
for x in lst:
|
||||||
|
print x
|
||||||
|
|
||||||
|
print lst
|
9
listintro.py
Normal file
9
listintro.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
odd_numbers_lst = [1,3,5,7]
|
||||||
|
even_number_lst = [2,4,6,8]
|
||||||
|
print odd_numbers_lst + even_number_lst # to concatenate two lists together
|
||||||
|
|
||||||
|
mixed_lst = [1, "lab1"]
|
||||||
|
print mixed_lst
|
||||||
|
|
||||||
|
lst = [1,2,3,4]
|
||||||
|
print lst*3
|
25
loopsinto.py
Normal file
25
loopsinto.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
even_number = [2,4,6,8] #loop and print list
|
||||||
|
for num in even_number:
|
||||||
|
print num
|
||||||
|
|
||||||
|
numbers = [1,2,3,4,5,6,7,8,9]
|
||||||
|
for num in numbers:
|
||||||
|
#check if number is even
|
||||||
|
if num %2 == 0:
|
||||||
|
print num
|
||||||
|
|
||||||
|
counter = 1
|
||||||
|
while counter < 10:
|
||||||
|
print counter
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
for x in range(10):
|
||||||
|
print(x)
|
||||||
|
if x==5:
|
||||||
|
break
|
||||||
|
|
||||||
|
import string
|
||||||
|
for letter in string.ascii_lowercase:
|
||||||
|
if letter == "a":
|
||||||
|
continue #print all letters except 'a'
|
||||||
|
print letter
|
21
matrixinto.py
Normal file
21
matrixinto.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
mymatrix = [[1,2,3], [4,5,6], [7,8,9]]
|
||||||
|
#to iterate through rows
|
||||||
|
for i in range(len(mymatrix)):
|
||||||
|
print i
|
||||||
|
print(mymatrix[i])
|
||||||
|
|
||||||
|
#ask user for the number of columns and rows, then creates the matrix
|
||||||
|
rows = int(raw_input("enter number of rows: "))
|
||||||
|
columns = int(raw_input("enter number of columns: "))
|
||||||
|
#create matrix
|
||||||
|
mymatrix = []
|
||||||
|
for i in range(0, rows):
|
||||||
|
myrow = [] #row has to be initialized ever iteration
|
||||||
|
for j in range(0, columns):
|
||||||
|
num = int(raw_input("enter a number: "))
|
||||||
|
myrow.append(num)
|
||||||
|
mymatrix.append(myrow)
|
||||||
|
|
||||||
|
for i in range(len(mymatrix)):
|
||||||
|
print i
|
||||||
|
print(mymatrix[i])
|
16
numberinputexercise.py
Normal file
16
numberinputexercise.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
num_list = []
|
||||||
|
while (True):dicks
|
||||||
|
num = eval(raw_input("Enter a Number: "))
|
||||||
|
if num == 0:
|
||||||
|
break
|
||||||
|
num_list.append(num)
|
||||||
|
|
||||||
|
print num_list
|
||||||
|
print sum(num_list)
|
||||||
|
print max(num_list)
|
||||||
|
print min(num_list)
|
||||||
|
|
||||||
|
product = 1
|
||||||
|
for numu in num_list:
|
||||||
|
product *= numu
|
||||||
|
print product
|
2
sort.py
Normal file
2
sort.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
list = ["aaaa", "ffff", "adadad", "cccc", 5, 2, 4]
|
||||||
|
print list.sort()
|
4
variableintro.py
Normal file
4
variableintro.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
num = 2
|
||||||
|
strn = "hello"
|
||||||
|
#print str + num
|
||||||
|
print strn + str(num)
|
Loading…
Reference in New Issue
Block a user