Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commit from Princess Assessment 3 and 4 #21

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
26 changes: 26 additions & 0 deletions assessment/assessment_3/Princess_Ogbechie/1a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This is a function called merge that takes 2 already sorted lists of possibly different lengths
# and merges them into a single sorted list using the sort method


def merge(): # defining the function
list1 = [] #initiate lists
list2 = []
while True:
print("Enter values for List 1. (Type . to end list)")
val1 = input()
if val1 == ".": # used to show the end of the list
break
list1 = list1 + [val1] # adds new values to the list
list1.sort() #sorting of the first list
while True:
print("Enter values for List 2. (Type . to end list)")
val2 = input()
if val2 == ".":
break
list2 = list2 + [val2]
list2.sort()
mergeList = list1 + list2 # list concatenation
mergeList.sort(key = str.lower) # sorting of the newly merged list in alpabetical order
print(mergeList, sep =", ")

merge()
29 changes: 29 additions & 0 deletions assessment/assessment_3/Princess_Ogbechie/2a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This is a function called primes that is given a number n and returns a list of the first n primes


import itertools # itertools module allows the use of .count function

def primes(n): # function definition

primeN = [] # initialize Prime number list
time = 0 # time here is used as a counter

for ANum in itertools.count(2,1): # itertools.count is used instead of range as there is no definite range till the counter is finished. It counts infinitely beginning from 2 in steos of 1
Prime = True

for num in range (2,ANum):
if ANum % num == 0: # if a number is perfectly divisible by other numbers except itself and 1
Prime = False # it is not prime

if Prime:
primeN.append(ANum) # this adds the prime number to the list
time += 1

if time == n :
break
print(primeN) # prints the list of n prime numbers

try:
primes(int(input("Enter a number: ")))
except ValueError : # if no value is entered, the function has a default value for the first 100 prime numbers.
primes(100)
25 changes: 25 additions & 0 deletions assessment/assessment_3/Princess_Ogbechie/2b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This is the modified primes function that allows the user enter what number the primes should start from and how many the user wants

import itertools

def primes(start, n): # uses 2 arguments to indicate start point and the number of primes
primeN = []
time = 0
for ANum in itertools.count(start,1): #itertools.count is used to count to infinity while the numbers are used by ANum to be divided by Num to find out if ANum can be divided by any number in between.
Prime = True
for num in range (2,ANum):
if ANum % num == 0:
Prime = False
if Prime:
primeN.append(ANum)
time += 1
if time == n :
break
print(primeN)

try:
x = int(input("Enter your first number. ")) # This value indicates the start point for the prime numbers
y = int(input("Enter number of primes you want. ")) # this is the number of the primes the user wants
primes(x, y)
except ValueError : # uses a default value of the first 100 prime numbers if no value is given
primes(2,100)
17 changes: 17 additions & 0 deletions assessment/assessment_3/Princess_Ogbechie/3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This is a function that given a string
# it returns a string with each uppercasse letter replaced by a lowercase letter and a lowercase letter replaced by an uppercase letter

def change_case(test):

for char in test: # Checks each character in the inputted string

if char.isupper(): # checks for uppercase letters
print(char.lower(), end = "") # switches to lowercase letter

if char.islower(): # checks for lowercase letters
print(char.upper(), end = "") # switches to uppercase letter

if char == " ": # leaves spaces where there are spaces
print(" ", end = "")

change_case(input("Enter your desired string: \n"))
19 changes: 19 additions & 0 deletions assessment/assessment_3/Princess_Ogbechie/4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This is a function called matches that takes two strings as arguments and returns
# how many matches there are between the strings.

# A match is where the two strings have the same character at the same index.

def matches(x,y):
count = 0 # initiates count
for i in range(len(x)) : # counts the length of the string and compares to the other
if x[i] == y[i] :
count += 1
else :
continue
print("The number of matches from your entered strings is " + str(count))


str1 = str(input("Please enter your first string:\n"))
str2 = str(input("Please enter your second string:\n"))

matches(str1,str2)
19 changes: 19 additions & 0 deletions assessment/assessment_3/Princess_Ogbechie/5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This function is that takes two strings and returns True if the strings are of the same length and differ in exactly one letter.

def one_away(x,y):
count = 0 # initialize counter
for i in range(len(x)): # checks and compares letters in both strings
if x[i] != y[i]:
count += 1
else:
continue
if len(x) == len(y) and count == 1: # conditional comparison statement
print("\nTrue")
else :
print("More than 1 letter differs.")


m = input("First string:\n")
n = input("Second string:\n")

one_away(m,n)
Empty file.
40 changes: 40 additions & 0 deletions assessment/assessment_4/Princess_Ogbechie/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This program asks the user to enter a list of integers and carries our some commands on them.

line = []

while True :
try : # Makes sure that only intergers are entered.
number = int(input("Enter a number: "))
line += [number]
except ValueError: #if anything other than an integer is entered, the list ends.
break


print("\nThe total number of items in the list is ", len(line)) # Tells user the total number of items in the list
print("\nThe last item in the list is ", line[len(line)-1]) # Tells user the last item in the list
line.reverse()
print("\nThis is the list in reverse order \n", line) # Prints the lists in the reverse order

if 5 in line: # checks if the list contains a 5
print("\nYes, there is a 5 in the list.")
else:
print("\nNo, there is no 5 in the list.")

count = 0
for i in range(len(line)): # Tells user the number of 5's in the list
if line[i] == 5 :
count += 1
print ("\nThe number of 5's in the list is ", count)

line.remove(line[0]) # removes the first item from the list
line.remove(line[len(line)-1]) #removes the last item from the ist

line.sort() # sorts the remaining items in the list
print("\nThis is the newly sorted list without the first and the last items\n", line) # Print new list

less = 0
for i in range(len(line)):
if line[i] < 5 : # prints the number of integers on list that are less than 5
less += 1
print ("\nThe number of integers in the list less than 5 are ", less)

27 changes: 27 additions & 0 deletions assessment/assessment_4/Princess_Ogbechie/2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This program generates a list of 20 random numbers betweeen 1 and 100 and carries out some instructions on it.

import random # imports module that calls random integers
import statistics # imports module that calls mean

ranNum = []

for i in range(20):
ranNum.append(random.randint(1,100))

print(ranNum) # print the list

print("\nThe average of the elements in the list above is ", round(statistics.mean(ranNum),2)) # Prints the average of all elements on the list

print(f"\nThe largest value on the list is {max(ranNum)}.\nThe smallest value on the list is {min(ranNum)}.") # Prints largest and smallest values on the list

ranNum.sort() # sorts the list

print(f"\nThe second largest value on the list is {ranNum[-2]}.\nThe second smallest value on the list is {ranNum[1]}") # Prints second largest value on the list and the second smallest value on the list based on their positions in the now sorted list

count = 0
for i in range(len(ranNum)-1):
if ranNum[i] % 2 == 0 : # Checks for even numbers
count += 1 # counts the even numbers in the list
else :
continue
print(f"\nThere are {count} even numbers in the list.")
19 changes: 19 additions & 0 deletions assessment/assessment_4/Princess_Ogbechie/3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# this program is given a start set of values in a list and instructions to be worked on

entry = [8,9,10] # initial set of values

entry[1] = 17 # set index 1 to 17

entry += [4,5,6] # add 4,5,6 to the end of the list

entry.remove(entry[0]) # remove the first entry from the list

entry.sort() # sort the list

entry *= 2 # Double the list

entry.insert(3,25) # insert 25 at index 3

print(entry)

# the final list should equal [4,5,6,25,10,17,4,5,6,10,17]
8 changes: 8 additions & 0 deletions assessment/assessment_4/Princess_Ogbechie/4a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#This program uses a for loop to print a list of the integers from 0 to 49

intList = []

for i in range (0,50):
intList += [i]

print(intList)
8 changes: 8 additions & 0 deletions assessment/assessment_4/Princess_Ogbechie/4b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This program uses a for loop to print a list containing the squares of integers 1 through 50

sqrList = []

for i in range (1,51):
sqrList += [i ** 2]

print(sqrList)
10 changes: 10 additions & 0 deletions assessment/assessment_4/Princess_Ogbechie/4c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Using a for loop to create a list ['a','bb','ccc',.......,] ending with 26 copies of the letter z

import string # imports the string module that allows the lowercase values to be used continuously.

lett = []

for i in string.ascii_lowercase: #the string.ascii_lowercase command gives a string of all the lowercase letters of the alphabet
lett += [i * ((string.ascii_lowercase.index(i)+1))] #the .index function works like with any other list to give the position of a particular letter

print(lett) # Output statement
33 changes: 33 additions & 0 deletions assessment/assessment_4/Princess_Ogbechie/5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This is a program that takes any 2 lists L and M of the same size and adds their elements to form list N
# Elements of list N are the sum of corresponding elements of L and M

size = int(input("Enter the size of the lists. "))
print()

count1 = 0 # initiaize variables and lists
count2 = 0
L = []
M = []
N = []

while True:
Num = int(input("Enter the values for list 1: ")) # User entering values
L.append(Num) #List concatenation
count1 += 1
if size == count1 :
print("\n")
break

while True:
Mum = int(input("Enter the values for list 2: "))
M.append(Mum)
count2 += 1
if size == count2 :
print("\n\n")
break

for i in range(size):
sum = L[i] + M[i] # adding values of corresponding elements
N += [sum]

print("The new list is ", N)