Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f90c2b8

Browse files
committedJul 30, 2021
add new examples
1 parent 1b77631 commit f90c2b8

10 files changed

+262
-0
lines changed
 

‎README 2.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
IT6033 Data Structure and Algorithm (Q3) Week-1 Session-2
2+
Perform the following programming activities using C# in visual studio. These activities are formulated to refresh the previously learned C# concepts
3+
Task 1:
4+
Given a temperature in Celsius degrees, write a method that converts it to Fahrenheit degrees or vice versa. Remember that temperature below -271.15°C (absolute zero) does not exist!
5+
Expected Input and Output:
6+
Temperature: 5
7+
Celsius (C) or Fahrenheit (F): F
8+
Conversion to Celsius: -15
9+
Temperature: -300
10+
Celsius (C) or Fahrenheit (F): C
11+
Note: Temperature below -271.15°C (absolute zero) does not exist on earth!
12+
Task 2:
13+
Given two integers, write a method that returns their multiplication if they are both divisible by 2 or 3, otherwise returns their sum.
14+
Expected input and output
15+
Enter two integers: 15, 30
16+
Divisible By 2 Or 3(15, 30) → 450
17+
Enter two integers: 2, 90
18+
Divisible By 2 Or 3(2, 90) → 180
19+
Enter two integers: 7, 12
20+
Divisible By 2 Or 3(7, 12) → 19
21+
Task 3: Write a program in C# Sharp to take 10 numbers as input from user and find their sum and average.
22+
Task 4: Write a program in C# Sharp to display the multiplication table of a given integer. Expected input and output Input the number (Table to be calculated): 15 Expected Output: 15 X 1 = 15 ... ... 15 X 10 = 150
23+
Task 5:
24+
Write a C# Sharp program to print on screen the output of adding, subtracting, multiplying and dividing of two numbers which will be entered by the user.
25+
Expected input and output
26+
Input the first number: 25 Input the second number: 4 Expected Output: 25 + 4 = 29 25 - 4 = 21 25 x 4 = 100 25 / 4 = 6 25 mod 4 = 1
27+
IT6033 Data Structure and Algorithm (Q3) Week-1 Session-2
28+
Task 6: Write a C# Sharp program that takes distance and time as input and displays the speed in kilometers per hour and miles per hour.
29+
Expected input and output
30+
Input distance (meters): 50000 Input time (hour): 1 Input time(minutes): 35 Input time(seconds): 56 Expected Output: Your speed in meters/sec is 8.686588 Your speed in km/h is 31.27172 Your speed in miles/h is 19.4355
31+
Task 7: Write a C# Sharp program that takes the radius of a sphere as input and calculate and display the surface area and volume of the sphere.
32+
Expected input and output
33+
Radius: 2 Expected Output: Surface Area of the Sphere: 50.26548 Volume of the Sphere: 33.51032
34+
Note: Try completing these activities during the session time and save the code in your folder.
35+
Prepare appropriate documents aligned to your assessment. These notes will prepare you for your Assessment.

‎boxProblem.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from math import floor
2+
3+
# get the user to imput a number
4+
items = int(input("Please enter the number of items: "))
5+
6+
# calculate the number of big items
7+
big = floor(items // 5)
8+
9+
# calculate the number of medium items
10+
medium = floor(items % 5 / 3)
11+
12+
# calculate the number of small items
13+
small = (items % 5) % 3
14+
15+
# calculate the total items
16+
total = big + medium + small
17+
18+
# print out the the boxes used
19+
print("total Boxes used: ", total)
20+
# print out the number of big boxes
21+
print("Number of big boxes used: ", big)
22+
23+
# print out the number of medium boxes
24+
print("Number of medium boxes used: ", medium)
25+
26+
# print out the number of small boxes
27+
print("Number of small boxes used: ", small)

‎calculateSphere.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# take the radius of a sphere as input
2+
# calculate and display:
3+
# the surface area
4+
# and volume of the sphere
5+
6+
# import math
7+
import math
8+
9+
10+
def main():
11+
print("This program calculates the surface area and volume of a sphere")
12+
r = eval(input("Please enter the radius of the sphere: "))
13+
s = 4 * math.pi * r ** 2
14+
v = 4 / 3 * math.pi * r ** 3
15+
# 2 decimal places
16+
print("The surface area of the sphere is:", round(s, 2))
17+
print("The volume of the sphere is:", round(v, 2))
18+
19+
20+
main()

‎capitalIndex.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Write a function named capital_indexes.
2+
#
3+
# The function takes a single parameter,
4+
# which is a string.
5+
#
6+
# Your function should return a list of all the indexes in the string that have capital letters.
7+
8+
def capital_indexes(str):
9+
return [i for i, letter in enumerate(str) if letter.isupper()]
10+
11+
12+
print(capital_indexes("HeLlo")) # => [0, 1, 2]

‎divisible2or3.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Given two integers
2+
# write a method that returns their multiplication
3+
# if they are both divisible by 2 or 3
4+
# otherwise returns their sum.
5+
6+
7+
def divisible2or3(a, b):
8+
if a % 2 == 0 and b % 2 == 0:
9+
return a * b
10+
elif a % 3 == 0 and b % 3 == 0:
11+
return a * b
12+
else:
13+
return a + b
14+
15+
# test cases:
16+
# Expected input and output
17+
18+
19+
# Enter two integers: 15, 30
20+
# Divisible By 2 Or 3(15, 30) → 450
21+
print(divisible2or3(15, 30) == 450)
22+
23+
# Enter two integers: 2, 90
24+
# Divisible By 2 Or 3(2, 90) → 180
25+
print(divisible2or3(2, 90) == 180)
26+
27+
# Enter two integers: 7, 12
28+
# Divisible By 2 Or 3(7, 12) → 19
29+
print(divisible2or3(7, 12) == 19)

‎formatNumber.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Write a function named format_number that takes a non-negative number as its only parameter.
2+
# Your function should convert the number to a string and add commas as a thousands separator.
3+
4+
def format_number(number):
5+
return "{:,}".format(number)
6+
7+
8+
print(format_number(1234))
9+
print(format_number(1234567890))

‎speedKphMph.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# take distance accepting either km/hr or mph
2+
# and time as input
3+
#
4+
# calculate the speed in both km/hr and mph
5+
# kilometers per hour and miles per hour.
6+
7+
print('This program will calculte traveling speed')
8+
print(f"you'll need to know the distance and time")
9+
print("and the unit of distance that you're using")
10+
11+
12+
def get_unit():
13+
unit = input(
14+
'what unit of distance are you using? [K]ilometers or [M]iles: ')
15+
# change this to a switch case statement?
16+
17+
if unit == 'K' or unit == 'k' or unit == 'Kilometers':
18+
unit = 'K'
19+
elif unit == 'm' or unit == 'M' or unit == 'Miles':
20+
unit = 'M'
21+
else:
22+
print('invalid unit')
23+
get_unit()
24+
return unit
25+
26+
27+
unit = get_unit()
28+
print(unit)
29+
30+
if(unit) == 'K':
31+
dist = float(input('what is the distance? '))
32+
time = float(input('what is the time? '))
33+
speed = dist/time
34+
speedMph = speed * 0.62137
35+
print(f'you are traveling {speed} km/hr')
36+
# the funny :.2f just means 2 decimal places
37+
print(f'Your speed in MPH is: {speedMph:.2f}')
38+
else:
39+
dist = float(input('what is the distance? '))
40+
time = float(input('what is the time? '))
41+
speed = dist/time
42+
speedKm = speed * 1.60934
43+
print(f'you are traveling {speed} mph')
44+
print(f'Your speed in KM is: {speedKm:.2f}')
45+
46+
47+
# dist = float(input('Enter distance in kilometers: '))
48+
# time = float(input('Enter time in hours: '))
49+
# speedKm = dist/time
50+
# speedMph = speedKm * 0.621371
51+
# print(f'Your speed in KPM is: {speedKm}')
52+
# # round to 2 decimal places
53+
# print(f'Your speed in MPH is: {speedMph:.2f}')
54+
55+
# dist = float(input('Enter distance in miles: '))
56+
# time = float(input('Enter time in hours: '))
57+
# speedM = dist/time
58+
# speedKm = speedM * 1.60934
59+
# print(f'Your speed in miles per hour is: {speedM}')
60+
# # round to 2 decimal places
61+
# print(f'Your speed in KPH is: {speedKm:.2f}')

‎sum10numbers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# take 10 numbers as input from user and find their sum and average
2+
3+
# makes more sense to generalise it to any number of numbers :)
4+
# define a function which takes n number of int as input
5+
def sum_avg(n):
6+
# take n numbers as input from user
7+
num_list = []
8+
for i in range(n):
9+
num = int(input("Enter a number: "))
10+
num_list.append(num)
11+
# find the sum of the numbers
12+
sum = 0
13+
for j in range(n):
14+
sum = sum + num_list[j]
15+
# find the average of the numbers
16+
avg = sum/n
17+
# print the sum and average
18+
print("The sum of the numbers is:", sum)
19+
print("The average of the numbers is:", avg)
20+
21+
22+
sum_avg(10)

‎temperature.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Given a temperature in Celsius
2+
# degrees,
3+
# write a method that converts it to Fahrenheit degrees or vice versa.
4+
# Remember that temperature below -271.15C (absolute zero) does not exist!
5+
6+
# convert celsius to fahrenheit
7+
def celsius_to_fahrenheit(celsius):
8+
if celsius < -273.15:
9+
return "That temperature does not exist!"
10+
# [(Celsius value) × (9/5)] + 32
11+
fahrenheit = (celsius * (9/5)) + 32
12+
return fahrenheit
13+
14+
# Expected Input and Output:
15+
# test function celcius to fahrenheit
16+
17+
18+
def test_celsius_to_fahrenheit():
19+
print(celsius_to_fahrenheit(-15) == "That temperature does not exist!")
20+
21+
# convert fahrenheit to celsius
22+
23+
24+
def fahrenheit_to_celsius(fahrenheit):
25+
if fahrenheit < -459.67:
26+
return "That temperature does not exist!"
27+
# formular for celsius from fahrenheit
28+
# [(Fahrenheit value) - (32)] * (5/9)
29+
celsius = (fahrenheit - 32) * (5/9)
30+
return celsius
31+
32+
33+
# test function fahrenheit to celsius
34+
# get some reliable data later
35+
print(celsius_to_fahrenheit(-15))

‎timesTable.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# display the multiplication table of a given integer.
2+
# Input the number (Table to be calculated): 15
3+
# Expected Output: 15 X 1 = 15 ... ... 15 X 10 = 150
4+
5+
def timesTable(number):
6+
for i in range(1, 11):
7+
print(number, "X", i, "=", number * i)
8+
9+
10+
# get the number to be multiplied
11+
number = int(input("Enter the number for the (Table to be calculated): "))
12+
timesTable(number)

0 commit comments

Comments
 (0)
Please sign in to comment.