name = 'Ng Chin Chia' # (1) REPLACE THE STRING VARIABLE WITH YOUR NAME in string type student_num = '7058901' # (2) REPLACE THIS STRING VARIABLE WITH YOUR UOW ID in string type subject_code = 'CSIT110' # (3) define solution here #Question 1 def hocusPocus(N): i = list(range(1,N)) for e in i: if (e % 9 == 0 and e % 5 == 0): e = "HocusPocus" elif (e % 5 == 0): e = "Hocus" elif (e % 9 == 0): e = "Pocus" print(e, end = ' ') print("") #Question 2 def question_2(): current_Age = int(input("Enter current age:")) if current_Age < 55: current_OA = float(input("Enter amount in OA:")) current_SA = float(input("Enter amount in SA:")) current_MA = float(input("Enter amount in MA:")) elif current_Age >= 55: current_OA = float(input("Enter amount in OA:")) current_SA = float(input("Enter amount in SA:")) current_MA = float(input("Enter amount in MA:")) current_RA = float(input("Enter amount in RA:")) rate_OA = (current_OA/100)*2.5 #interest rate for Ordinary rate_SA = (current_SA/100)*4 #interest rate for Special rate_MA = (current_MA/100)*4 #interest rate for Medisave if current_Age < 55 and current_OA <= 20000: total_fund = current_OA + current_SA + current_MA #total amount rate_fund = total_fund/100 #interest rate for total amount rate_total = rate_OA + rate_SA + rate_MA + rate_fund #total rates elif current_Age < 55 and current_OA > 20000: temp = current_OA - 20000 total_fund = current_OA + current_SA + current_MA - temp #total amount rate_fund = total_fund/100 #interest rate for total amount rate_total = rate_OA + rate_SA + rate_MA + rate_fund #total rates elif current_Age >= 55 and current_OA <= 20000: rate_RA = (current_RA/100)*4 #interest rate for Retirement total_fund = current_OA + current_SA + current_MA + current_RA #total amount if total_fund > 30000: rate_fund1 = (30000/100)*2 rate_fund2 = ((total_fund-30000)/100) rate_total = rate_OA + rate_SA + rate_MA + rate_RA + rate_fund1 + rate_fund2 #total rate elif total_fund == 30000: rate_fund1 = (30000/100)*2 rate_total = rate_OA + rate_SA + rate_MA + rate_RA + rate_fund1 #total rates else: rate_fund1 = (total_fund/100)*2 rate_total = rate_OA + rate_SA + rate_MA + rate_RA + rate_fund1 #total rates elif current_Age >= 55 and current_OA > 20000: temp = current_OA - 20000 rate_RA = (current_RA/100)*4 #interest rate for Retirement total_fund = current_OA + current_SA + current_MA + current_RA - temp #total amount if total_fund > 30000: rate_fund1 = (30000/100)*2 rate_fund2 = ((total_fund-30000)/100) rate_total = rate_OA + rate_SA + rate_MA + rate_RA + rate_fund1 + rate_fund2 #total rate elif total_fund == 30000: rate_fund1 = (30000/100)*2 rate_total = rate_OA + rate_SA + rate_MA + rate_RA + rate_fund1 #total rates else: rate_fund1 = (total_fund/100)*2 rate_total = rate_OA + rate_SA + rate_MA + rate_RA + rate_fund1 #total rates print('Your interest rate this year will be',"${:.2f}".format(rate_total)) #Question 3 def question_3(): input_single = int(input("Number of Single rooms: ")) input_twin = int(input("Number of Twin rooms: ")) input_deluxe = int(input("Number of Deluxe rooms: ")) input_suite = int(input("Number of Suite rooms: ")) input_stay = int(input("Length of stay(number of nights): ")) single_rm = 90 twin_rm = 150 deluxe_rm = 250 suite_rm = 1050 subtotalRm = input_single + input_twin + input_deluxe + input_suite compute_single = single_rm * input_single * input_stay compute_twin = twin_rm * input_twin * input_stay compute_deluxe = deluxe_rm * input_deluxe * input_stay compute_suite = suite_rm * input_suite * input_stay compute_subtotal = compute_single + compute_twin + compute_deluxe + compute_suite compute_total = compute_subtotal+(compute_subtotal/100)*7 a = "$" b = str("{:.2f}".format(compute_single)) s = a + b aa = "$" bb = str("{:.2f}".format(compute_twin)) t = aa + bb aaa = "$" bbb = str("{:.2f}".format(compute_deluxe)) d = aaa + bbb aaaa = "$" bbbb = str("{:.2f}".format(compute_suite)) st = aaaa + bbbb aaaaa = "$" bbbbb = str("{:.2f}".format(compute_subtotal)) subtotal = aaaaa + bbbbb aaaaaa = "$" bbbbbb = str("{:.2f}".format(compute_total)) total = aaaaaa + bbbbbb #aligned_string = "{:<10}".format(first_col) print("\nSummary of your booking for", input_stay, "night(s)") print("Single room".ljust(13), str(input_single).center(3), s.rjust(10) ) print("Twin room".ljust(13), str(input_twin).center(3), t.rjust(10) ) print("Deluxe room".ljust(13), str(input_deluxe).center(3), d.rjust(10) ) print("Suite room".ljust(13), str(input_suite).center(3), st.rjust(10) ) print("Subtotal".ljust(13), str(subtotalRm).center(3), subtotal.rjust(10) ) print("Total(7% g.s.t)".ljust(16), total.rjust(11) ) def recover_files(): import re empList = [] while True: inp = input("Filename?") if inp == "": break r = re.sub(r'{[^}]*}','',inp) empList.append(r) print(empList, sep=',') def main(): print("Assignment2") # (4)test code here #Question 1 test code result = hocusPocus(46) print (result) #Question 2 test code #question_2() #Question 3 test code #question_3() recover_files() if __name__ == '__main__': ## DO NOT EDIT THESE TWO LINES. main()
Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.
OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.
import sys
name = sys.stdin.readline()
print("Hello "+ name)
Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.
When ever you want to perform a set of operations based on a condition IF-ELSE is used.
if conditional-expression
#code
elif conditional-expression
#code
else:
#code
Indentation is very important in Python, make sure the indentation is followed correctly
For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.
mylist=("Iphone","Pixel","Samsung")
for i in mylist:
print(i)
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while condition
#code
There are four types of collections in Python.
List is a collection which is ordered and can be changed. Lists are specified in square brackets.
mylist=["iPhone","Pixel","Samsung"]
print(mylist)
Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
Below throws an error if you assign another value to tuple again.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)
Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.
myset = {"iPhone","Pixel","Samsung"}
print(myset)
Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.
mydict = {
"brand" :"iPhone",
"model": "iPhone 11"
}
print(mydict)
Following are the libraries supported by OneCompiler's Python compiler
Name | Description |
---|---|
NumPy | NumPy python library helps users to work on arrays with ease |
SciPy | SciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation |
SKLearn/Scikit-learn | Scikit-learn or Scikit-learn is the most useful library for machine learning in Python |
Pandas | Pandas is the most efficient Python library for data manipulation and analysis |
DOcplex | DOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling |