Professional Documents
Culture Documents
if sub in data:
print("Valid")
else:
print("Invalid")
#Encryption
plainText = input ("Enter a one-word, lowercase message:")
distance = int (input("Enter the distance value:"))
code = ""
for ch in plainText:
ordvalue = ord(ch)
cipherValue = ordvalue + distance
if cipherValue > ord ("z"):
cipherValue = ord ("a") + distance - (ord("z") - ordvalue+1)
code += chr(cipherValue)
print(code)
#Decryption
plainText = input ("Enter the coded text:")
distance = int (input("Enter the distance value:"))
code = ""
for ch in plainText:
ordvalue = ord(ch)
cipherValue = ordvalue - distance
if cipherValue > ord ("z"):
cipherValue = ord ("a") - distance - (ord("z") - ordvalue+1)
code += chr(cipherValue)
print(code)
#Decimal to Binary
decimal = int(input("Enter a decimal integer:"))
if decimal == 0:
print(0)
else:
print("Quotient Remainder Binary")
bitstring = ""
while decimal > 0:
remainder = decimal % 2
decimal = decimal // 2
bitstring = str(remainder) + bitstring
print("%5d%8d%12s" % (decimal, remainder, bitstring))
print("The binary representation is", bitstring)
#Binary to Decimal
bitstring = input("Enter a string of bits:")
decimal = 0
exponent = len(bitstring) - 1
for digit in bitstring:
decimal = decimal + int(digit) * 2 ** exponent
exponent = exponent - 1
print ("The integer value is", decimal)
#Password validation
password = input("Enter a password. ")
digits = 0
letters = 0
for ch in password:
if ch.isalpha():
letters = letters + 1
elif ch.isalnum():
digits = digits + 1
else:
print("Invalid password!")
exit()
break
#1 Program displays Valid SSN for a correct Social Security number or Invalid
SSN otherwise.
password = (input("Enter SSN.\n"))
data = password
digits = 0
print ((data[0:3]) + "-" + data[3:5] + "-" + data[5:9])
for d in password:
if d.isnumeric():
digits = digits + 1
else:
print("Invalid SSN!")
exit()
break
if digits == 9:
print("Valid SSN!")
else:
print("Invalid SSN!")
#2
numbers = [5,4,3,2,1]
print (numbers)
print (numbers[1]+7)
#3
x = 10
var = [x, x+1, x+2, x+3, x+4, x+5]
print(var)
#5
string_list = list("Bang")
print (string_list)
#6
ex = [8,5,4,7,9,2,5]
ex.insert(4,526)
ex.extend([880,90])
ex.append(9)
ex.pop (9)
ex.sort()
print (ex)
#7
y = list (range(3,31,3))
print(y)
for x in y.copy():
if x%2 != 0:
y.remove(x)
print(y)
#8
num = [1,2,3,4,5]
if 3 in [num.copy]:
print (len(num))
print("True")
else:
print("False")
#9
list = [34,45,67]
target =467
if target in list:
print(list.index(target))
else:
print(8)
numbers.sort()
midpoint = len (numbers)//2
print("The median is:")
if len(numbers) % 2 ==1:
print(numbers[midpoint])
else:
print ((numbers[midpoint] + numbers [midpoint-1])/2)
#10
students = {}
students["name"] = input("Enter a name. \n")
students["program"] = input("Enter a grade level. \n")
print(students)
students.pop("name")
print(students)
WEEK 3
#1
def info():
print ("Bang")
print ("A166")
print("ECE")
info()
#1.1
def my_function(country = "Philippines"):
print("I am from" ,country)
print(my_function("Sweden"))
print(my_function("Italy"))
print(my_function("Singapore"))
my_function()
#2
x = "Hakdog"
def hehe():
y = "Welcome"
print (x)
print (y)
hehe()
#3
name = input("Enter name. \n")
def greet (name):
print ("Hello " + name)
greet ("Boang")
greet(name)
greet(name)
#4
def my_function(x):
return 5*x
print(my_function(3))
print(my_function(4))
print(my_function(9))
#4.1
no_1 = int(input("enter the first number: "))
no_2= int(input("enter the second number: "))
def math(x, y):
a = x + y
b = x - y
c = x * y
d = x / y
print("the sum of", x, "and", y, "is", a)
print("the difference of", x, "and", y, "is", b)
print("the product of", x, "and", y, "is", c)
print("the quotient of", x, "and", y, "is", d)
math(no_1,no_2)
#5
no_1 = int(input("Enter first number.\n"))
no_2 = int(input("Enter second number.\n"))
def add (x, y):
a = x + y
print ("The sum of " , x, "and", y, "is", a, ".")
add(no_1, no_2)
#6
num = int (input("Enter a number:"))
def multiply(x):
y = x * 5
return y
print (multiply(num))
#7
a = int (input("Enter first side. \n"))
b = int (input("Enter second side. \n"))
c = int (input("Enter third side. \n"))
print_this(a, b, c)
#8
num1 = int (input("Enter first number. \n"))
num2 = int (input("Enter second number. \n"))
def sum(x, y):
add = x + y
return add
#10
sec = int(input("Enter time in seconds. \n"))
def conv (x):
conv_min = x // 60
conv_sec = x%60
print ("Conversion:", conv_min, "minutes and",conv_sec,"seconds")
conv(sec)
#11
year = int(input("Enter number of months. \n"))
def conv (x):
conv_year = x // 12
conv_week = x%12
print ("Conversion:", conv_year, "year/s and", conv_week, "weeks.")
conv(year)
LAB 1
#1 Program displays Valid SSN for a correct Social Security number or Invalid
SSN otherwise.
#2 Program that prompt the user to enter a file name and a string to be
removed.
filename = input("Enter a filename.\n")
textfile = open (filename, "r")
Read = textfile.read()
textfile.close()
string = input ("Enter the string to be removed.\n")
if string in Read:
Read = Read.replace (string, str (""))
textfile = open (filename, "a")
textfile.close()
print ("Done")
#3 Program prompts the user to enter a password and displays valid password
#Password must have at least eight characters.
#A password must consist of only letters and digits.
#A password must contain at least two digits.
for ch in password:
if ch.isalpha():
letters = letters + 1
elif ch.isalnum():
digits = digits + 1
else:
print("Invalid password!")
exit()
break
if letters >= 8 and digits >= 2:
print("Valid password!")
else:
print("Invalid Password!")
if text in readstrings:
readstrings = readstrings.replace(text,replace)
fileopen = open(filename, 'w')
fileopen.write(readstrings)
fileopen.close()
print("Done")
else:
print("Error!")
LAB 2
#Reads a list of scores and then assigns grades based on the following scheme:
def grade(score, best):
if score >= best - 10:
return 'A'
if score >= best - 20:
return 'B'
if score >= best - 30:
return 'C'
if score >= best - 40:
return 'D'
return 'F'
score = input("Enter scores: ")
scores = [int(s) for s in score.split()]
best = scores[0]
for i in range(len(scores)):
if scores[i] > best:
best = scores[i]
for i in range(len(scores)):
print("Student", i, "score is", scores[i], "and grade is",
grade(scores[i], best))
convert(temp)
#10