Initial work

This commit is contained in:
bluepython508
2023-11-01 08:55:40 +00:00
parent 996ea45153
commit 688d1ec426
48 changed files with 1148 additions and 0 deletions

5
CS1032/assesments Normal file
View File

@@ -0,0 +1,5 @@
Type of Assessment |ILOs (7)|% Weighting|Release Date|Due Time|Due Date |C6|Feedback Due |Feedback Provided
Online MCQ Test 1 (1hour/48hour)|1,2,3,5 |20 |09/10/2023 |23:59 |11/10/2023 | |25/10/2023 |
Programming Exercise |7 |30 |16/10/2023 |23:59 |06/11/2023 | |20/11/2023 |
Exam (2h, on campus, MCQ) |4,5,6 |50 | | |Assessment Weeks| |After Exam Mtg.|
|100 | | | | | | |

2
CS1032/notes-2023-09-21 Normal file
View File

@@ -0,0 +1,2 @@
Look into industry projects

1
CS1032/notes-2023-09-28 Normal file
View File

@@ -0,0 +1 @@

1
CS1032/notes-2023-10-05 Normal file
View File

@@ -0,0 +1 @@

1
CS1032/notes-2023-10-06 Normal file
View File

@@ -0,0 +1 @@

1
CS1032/notes-2023-10-12 Normal file
View File

@@ -0,0 +1 @@

1
CS1032/notes-2023-10-13 Normal file
View File

@@ -0,0 +1 @@

0
CS1032/notes-2023-10-26 Normal file
View File

1
CS1032/notes-2023-10-27 Normal file
View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,4 @@
(def names ["Monkey" "Rooster" "Dog" "Pig" "Rat" "Ox" "Tiger" "Hare" "Dragon" "Snake" "Horse" "Sheep"])
(defn main [_ arg] (print (names (% (scan-number arg) 12))))

View File

@@ -0,0 +1,15 @@
(def grades {
"A+" "4.0"
"A" "4.0"
"A-" "3.7"
"B+" "3.3"
"B" "3.0"
"B-" "2.7"
"C+" "2.3"
"C" "2.0"
"C-" "1.7"
"D+" "1.3"
"D" "1.0"
})
(defn main [_ arg] (print (or (grades arg) "Not a grade")))

View File

@@ -0,0 +1,8 @@
(def old (peg/compile ~(* (repeat 3 (range "AZ")) " " (repeat 3 (range "09")) -1)))
(def new (peg/compile ~(* (repeat 4 (range "09")) " " (repeat 3 (range "AZ")) -1)))
(defn main [_ arg] (print (cond
(peg/match old arg) "Old"
(peg/match new arg) "New"
true "Not a license plate"
)))

View File

@@ -0,0 +1,117 @@
(def grammar (peg/compile ~{
:main (* :tok (any (* :s+ :tok)) -1)
:tok (+ (<- :word) (number (some (+ (range "09") "."))))
:word (* :init-char (any :rest-char))
:init-char (+ (range "AZ") (range "az") (set ":/{}><,.[]+=-_*&^%$#@!;()"))
:rest-char (+ :init-char (range "09"))
}))
# State @{ :dictionary @{ word { :definition [word] :immediate } } :stack @[] :mode fn }
(defn immediate [state word]
(cond
(or (number? word) (array? word) (tuple? word)) (array/push (state :stack) word)
(let
(word (or ((state :dictionary) word) (error (string "No such word defined: " word))))
((word :definition) state)
)))
(defn deferred [state word] (let
(definition ((state :dictionary) word)) (if (and definition (definition :immediate))
(immediate state word)
(array/push (array/peek (state :stack)) word)
)))
(defmacro fixed-word [args ret] ~{
:definition (fn [state]
(do
,;(map (fn [name] ~(def ,name (,array/pop (state :stack)))) (reverse args))
(array/concat (state :stack) ,ret)
)
)
})
(defn run-state [state words]
(loop [word :in words] (:mode state word))
)
(defn word-as [& words] { :definition (fn [state] (run-state state words)) })
(defn truthy [a] (and a (not (= a 0))))
(def std-dict @{
"pick" (fixed-word [a b] [a b a])
"drop" (fixed-word [a] [])
"swap" (fixed-word [a b] [b a])
"dup" (fixed-word [a] [a a])
"-" (fixed-word [a b] (- a b))
"+" (fixed-word [a b] (+ a b))
"*" (fixed-word [a b] (* a b))
"/" (fixed-word [a b] (/ a b))
"=" (fixed-word [a b] (= a b))
">" (fixed-word [a b] (> a b))
"<" (fixed-word [a b] (< a b))
"len" (fixed-word [a] (length a))
"true" (fixed-word [] true)
"false" (fixed-word [] false)
"dec" (word-as 1 "-")
"inc" (word-as 1 "+")
"negate" (word-as 0 "swap" "-")
"." (fixed-word [a] (do (print a) []))
".s" { :definition (fn [state] (pp (state :stack)))}
"{" { :definition (fn [state]
(array/push (state :stack) @[])
(set (state :mode) deferred)
)}
"}" { :definition (fn [state]
(def words (array/peek (state :stack)))
(if (> (count |(= $ "{") words) (count |(= $ "}") words))
(array/push words "}")
(do
(def dictionary (table/clone (state :dictionary)))
(array/pop (state :stack)) # Remove wordlist that's on stack top
(array/push (state :stack) (fn [state &opt recur]
(when recur (set (dictionary "recur") { :definition recur }))
(def state @{ :dictionary dictionary :stack (state :stack) :mode immediate })
(run-state state words)
))
(set (state :mode) immediate)
)
)
) :immediate true }
"()" { :definition (fn [state] ((array/pop (state :stack)) state)) }
":" { :definition (fn [state]
(set (state :mode) (fn [state word]
(def exec (array/pop (state :stack)))
(set ((state :dictionary) word) { :definition (fn [state] (exec state exec)) })
(set (state :mode) immediate)
))
)}
"if" (fixed-word [cond when-true when-false] (do ((if (truthy cond) when-true when-false) state) []))
"loop" { :definition (fn [state]
(def body (array/pop (state :stack)))
(body state)
(while (truthy (array/pop (state :stack))) (body state))
)}
"while" { :definition (fn [state]
(def body (array/pop (state :stack)))
(while (truthy (array/pop (state :stack))) (body state))
)}
"repeat" (fixed-word [count body] (for x 0 count (body state)))
"[" { :immediate true :definition (fn [state]
(set (state :mode) immediate)
)}
"]" { :definition (fn [state]
(def val (array/pop (state :stack)))
(array/push (array/peek (state :stack)) val)
(set (state :mode) deferred)
)}
})
(defn main [_ arg] (let (
words (peg/match grammar arg)
state @{ :dictionary (table/clone std-dict) :stack @[] :mode immediate }
)
(run-state state words)
))

View File

@@ -0,0 +1,37 @@
based on user input, mapping "a" to "you chose the first option", "b" to "you chose the second option",
"c" to "you chose the third option", otherwise "invalid choice"
# getting user input
option = input ("Please enter a value for choice: ")
#verifying equality to "a"
if option == "a":
# printing message
print (“you choose the first option”)
# equality to b
elif option == "b":
# printing message
print (“you choose the second option”)
# equality to c
elif option == "c":
# printing message
print (“you choose the third option”)
#otherwise
else:
# printing message
print("Invalid choice.")
Task 2
x is undefined - prepend "x = int(input())\n"
# if x is non-negative
# if x less than 10
# print message
Task 3
x is undefined - prepend "x = int(input())\n"
# if x is positive
# increase x, inform user
# otherwise
# decrease x, inform user
# print message

View File

@@ -0,0 +1,14 @@
from typing import Iterable
def split(delim: str, s: str) -> Iterable[str]:
while len(s):
idx = s.find(delim)
if idx == -1: idx = len(s)
yield s[:idx]
s = s[idx + len(delim):]
for x in split(", ", "Aberdeen, Dundee, Edinburgh, Glasgow"): print(x)
# Swap M and D names in the definitions
d, m, y = tuple(split("/", "11/9/2001"))
print(f"Day = {d}, Month = {m}, Year = {y}")

View File

@@ -0,0 +1,7 @@
def trapezium_area(w1, w2, h):
return h * (w1 + w2) / 2
w1 = int(input("Width of the top: "))
w2 = int(input("Width of the bottom: "))
h = int(input("Height: "))
print(f"Area is {trapezium_area(w1, w2, h)}")

View File

@@ -0,0 +1,20 @@
def bmi(weight, height):
return weight * height ** -2
def bmi_category(bmi):
if bmi < 18.5:
return "underweight"
elif bmi < 25:
return "healthy"
elif bmi < 30:
return "overweight"
elif bmi < 40:
return "obese"
else:
return "severely obese"
weight = float(input("Weight? "))
height = float(input("Height? "))
bmi = bmi(weight, height)
print(f"BMI is {bmi:.3}")
print(f"The BMI is in the {bmi_category(bmi)} category")

View File

@@ -0,0 +1,7 @@
def exp(n, p):
if p == 0: return 1
return n * exp(n, p - 1)
base = float(input("Base? "))
power = int(input("Exponent? "))
print(exp(base, power))

View File

@@ -0,0 +1,9 @@
import random
randnumber = random.randint(1, 50)
while True:
mynumber = int(input("Please enter a value: "))
if randnumber == mynumber:
print("[perfect guess, you won....")
break

View File

@@ -0,0 +1,2 @@
for i in range(10, 0, -1):
print(i)

View File

@@ -0,0 +1,4 @@
# If the example output is intended
terms = int(input("Enter the Total Number of Terms:\n"))
for i in range(16):
print(f"2 raised to the power {i} is {2 ** i}")

View File

@@ -0,0 +1,13 @@
counts = { 'chuck': 1, 'annie': 42, 'jan': 100 }
for key in counts:
if (count := counts[key]) > 1:
print(key, count)
lst = list(counts.keys())
print(lst)
lst.sort()
for key in lst:
print(key, counts[key])