Compare commits

...
22 Commits
Author SHA1 Message Date
RustyRaptor d0fa77d041 did it in 1 line instead of 2 lol 2019-12-05 14:00:43 -07:00
RustyRaptor 586dc7dd90 FINISHED LAB 11 2019-12-05 13:53:07 -07:00
RustyRaptor 4d63e2891d LAB10 2019-11-22 06:40:25 +00:00
RustyRaptor 7be7e53442 Started Lab 10 2019-11-18 17:47:44 -07:00
Ziad Arafat 4983b7ea1a FInished lab 9 2019-11-14 22:22:46 -07:00
RustyRaptor 03afd42f6f Started Lab 9 2019-11-05 13:02:30 -07:00
RustyRaptor 4f75526f2b Finished Lab 8 2019-11-04 16:54:54 -07:00
RustyRaptor 50793bfea1 Finished Lab 8 2019-10-30 15:35:47 -06:00
RustyRaptor 97176ee5ae finished lab 7 2019-10-21 17:20:23 -06:00
RustyRaptor 4f2f0727b6 added md for lab6 2019-10-17 13:48:23 -06:00
RustyRaptor 23bf4c7a41 Finished Lab 6 2019-10-17 13:48:00 -06:00
RustyRaptor 84741db0dc start lab6 2019-10-14 15:16:45 -06:00
RustyRaptor 5d5ca75b9f lab 5 in progress 2019-10-09 13:23:16 -06:00
RustyRaptor 4d990bea09 start lab5 2019-10-09 12:50:10 -06:00
RustyRaptor 5e0b590f77 finished lab4 2019-10-03 16:42:16 -06:00
RustyRaptor 23b2169be0 finished lab4 2019-10-03 16:41:02 -06:00
RustyRaptor 83f102577e added lab3 2019-09-23 13:17:55 -06:00
RustyRaptor 2c9582065c fixes 2019-09-09 18:18:54 -06:00
RustyRaptor 67db7b83e9 moving to FnT 2019-09-09 16:28:10 -06:00
RustyRaptor 08d0924b6a Merge branch 'master' of https://github.com/RustyRaptor/CS278 2019-09-09 14:13:28 -06:00
RustyRaptor a2aee877f8 added labs 2019-09-09 14:13:10 -06:00
RustyRaptor c3d13acc87 updated compile instructions for non-lab PCs 2019-08-26 19:13:51 -06:00
24 changed files with 668 additions and 0 deletions

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
# CS 278 Lab 7
Done in python
## How to Run on CS Machines
```
cd <source folder>
python3 lab7.py
```
+41
View File
@@ -0,0 +1,41 @@
import itertools
values = []
def bin_count(size):
for b in itertools.product([0, 1], repeat=size):
yield b
bin_list = list(bin_count(3))
bin_list.reverse()
print(
"This program finds Boolean expressions (in CNF and/or DNF)\n that have a "
"given input/output table")
print("Please enter values (0 or 1) for each row of the input/output table.")
for i in bin_list:
print("p=", i[0], ", q=", i[1], ", r=", i[2], ".")
values.append(input(" Truth value is (1/0): "))
print()
print("DNF expression for the input/output table is" if "1" in values else "")
for idx, val in enumerate(values):
if val == "1":
vals = bin_list[idx]
p = "p" if vals[0] == 1 else "p'"
q = "q" if vals[1] == 1 else "q'"
r = "r" if vals[2] == 1 else "r'"
plus = " +" if idx != len(values) - 1 else " "
print(p + q + r + plus, end=" ")
print("\nCNF expression for the input/output table is" if "0" in values
else "")
for idx, val in enumerate(values):
if val == "0":
vals = bin_list[idx]
p = "(p+" if vals[0] == 0 else "(p'+"
q = "q+" if vals[1] == 0 else "q'+"
r = "r)" if vals[2] == 0 else "r')"
print(p + q + r + " ", end=" ")
+30
View File
@@ -0,0 +1,30 @@
vals = input("Please enter values of a, b, c, and M in this order (separated "
"by spaces): ")
vals = vals.split(" ")
vals = [int(x) for x in vals]
begin_cycle = False
flag = 0
cycles = 0
start = vals[2]
start2 = vals[2]
for i in range(0, vals[3] - 1):
if begin_cycle:
cycles += 1
if flag == start:
begin_cycle = False
break
if i == vals[3] / 4:
flag = start
begin_cycle = True
e = "\n" if (i + 1) % 10 == 0 and i > 0 else " "
start = (vals[0] * start + vals[1]) % vals[3]
for j in range(0, 100):
e = "\n" if (j + 1) % 10 == 0 and j > 0 else " "
print(start2, end=e)
start2 = (vals[0] * start2 + vals[1]) % vals[3]
print("\nCycle length is", cycles)
+11
View File
@@ -0,0 +1,11 @@
# CS 278 Lab 8
Done in python
## How to Run on CS Machines
```
cd <source folder>
python3 lab8.py
```
+11
View File
@@ -1,6 +1,17 @@
# How to compile my code
## Lab Linux PCs
1. download to a folder on a lab PC or any Linux PC with gcc such as Documents
2. Open a terminal and cd to the code folder
- ```cd ~/Documents```
3. run ```gcc truthtable.c -o truthtable -Wall -Werror```
4. run ```./truthtable ```
## Windows (for TAs working at home)
1. go to this url
- https://repl.it/@RustyRaptor/CS278-Lab-1
2. Click the "Run" button
3. Compare the code to what I submitted in canvas.
4. Or just paste the code in canvas and run again
### Alternatively
- use Visual Studio or whatever you have on windows for C/C++
BIN
View File
Binary file not shown.
+24
View File
@@ -0,0 +1,24 @@
in_data = input("Please enter 4-digit code: ")
while True:
data_list = [char for char in in_data]
first_digit = 0
second_digit = 0
in_guess = input("Please enter a guess or 0 to stop")
if in_guess == "0":
break
guess_list = [char for char in in_guess]
score1 = 0
for key, val in enumerate(guess_list):
if guess_list[key] == data_list[key]:
guess_list[key] = 0
data_list[key] = 0
score1 += 10
for key, val in enumerate(guess_list):
for idx, i in enumerate(data_list):
if i == guess_list[key] and guess_list[key] != 0 and i != 0:
score1 += 1
guess_list[key] = 0
data_list[idx] = 0
print("Score: ", score1)
+11
View File
@@ -0,0 +1,11 @@
# CS 278 Lab 11
Done in python3
## How to Run on CS Machines
```
cd <source folder>
python3 lab11.py
```
+56
View File
@@ -0,0 +1,56 @@
import itertools
def convert_tuple(tup):
new_list = []
for i in tup:
new_list.append(str(i))
new_string = ''.join(new_list)
return new_string
def refer(code, guess):
data_list = [char for char in code]
guess_list = [char for char in guess]
score1 = 0
for key2, val2 in enumerate(guess_list):
if guess_list[key2] == data_list[key2]:
guess_list[key2] = 0
data_list[key2] = 0
score1 += 10
for key2, val2 in enumerate(guess_list):
for idx, i in enumerate(data_list):
if i == guess_list[key2] and guess_list[key2] != 0 and i != 0:
score1 += 1
guess_list[key2] = 0
data_list[idx] = 0
return score1
sequences = [x for x in itertools.product(list(range(1, 7)), repeat=4)]
possible = [True] * len(sequences)
start = 0
turns = 0
print("Please think of a 4 digit code with numbers between 1 and 6 and let me "
"guess it.")
while True:
print("There are", possible.count(True), "possible guesses. I am "
"guessing",
convert_tuple(sequences[start]))
turns += 1
score = int(input("Please enter the score"))
if score == 40:
print("Found in ", turns, "Turns")
print("Thank you for playing with me!")
break
else:
possible[start] = False
for key, val in enumerate(sequences):
if refer(convert_tuple(sequences[start]), convert_tuple(val)) != score:
possible[key] = False
if possible.count(True) == 0:
print("No more possibilities, please try again")
break
start = possible.index(True)
+68
View File
@@ -0,0 +1,68 @@
# CS 278 Lab 2
- Programmed in Python3
## How to Run on CS Machines
```
cd <source folder>
python3 lab2.py
```
## Last Output
```
/home/ugrad22/zarafat/PycharmProjects/cs278lab2/venv/bin/python /home/ugrad22/zarafat/PycharmProjects/cs278lab2/lab2.py
Proposition 1
p q r (~p → q) and (r → p)
F F F
F F T
F T F
F T T
T F F
T F T
T T F
T T T
Truths: 5
Falsehoods: 3
The proposition is Neither a Tautology or a Contradiction
____________________
Proposition 2
p q r (p or (~q)) and (r or ~(p → q))
F F F
F F T
F T F
F T T
T F F
T F T
T T F
T T T
Truths: 4
Falsehoods: 4
The proposition is Neither a Tautology or a Contradiction
____________________
Proposition 3
p q (p or (~q)) and (r or ~(p → q))
F F
F T
T F
T T
Truths: 4
Falsehoods: 0
The proposition is a Tautology
____________________
Proposition 4
p q (p or (~q)) and (r or ~(p → q))
F F
F T
T F
T T
Truths: 0
Falsehoods: 4
The proposition is a Contradiction
____________________
Process finished with exit code 0
```
+103
View File
@@ -0,0 +1,103 @@
def truth_gen(vars_count):
"""
Generate a 2D list of truth values for N variables
:param vars_count: Number of variables in the proposition
:return: A 2d list of all the possible values for the variables
"""
rows = 2 ** vars_count
table = []
bin_to_str = {
'0': 'F',
'1': 'T',
' ': 'F'
}
for i in range(rows):
bins = "{0:>{1}}".format("{0:b}".format(i), vars_count)
table.append([])
for j, val in enumerate(bins):
table[i].append(bin_to_str[val])
return table
# Logical operators
def _not(i):
return 'T' if i is 'F' else 'F'
def _or(i, j):
return 'T' if i is 'T' or j is 'T' else 'F'
def _and(i, j):
return 'T' if i is 'T' and j is 'T' else 'F'
def impl(i, j):
return 'F' if i is 'T' and j is 'F' else 'T'
# Propositions
def prop1(args=None):
if args is None:
args = [0, 0, 0]
p, q, r = tuple(args)
return _and((impl(_not(p), q)), (impl(r, p))), "(~p → q) and (r → p)"
def prop2(args=None):
if args is None:
args = [0, 0, 0]
p, q, r = tuple(args)
return _and((_or(p, _not(q))),
(_or(r, _not(impl(p, q))))), "(p or (~q)) and (r or ~(p → q))"
def prop3(args=None):
if args is None:
args = [0, 0]
p, q = tuple(args)
return impl(p, (impl((_not(_and(p, _not(q)))),
(_and(p, q))))), "(p or (~q)) and (r or ~(p → q))"
def prop4(args=None):
if args is None:
args = [0, 0]
p, q = tuple(args)
return _and((_and(p, (impl(p, q)))),
(_not(q))), "(p or (~q)) and (r or ~(p → q))"
def apply_prop(prop_function, vars_count):
"""
Function to apply proposition functions to a truth table
:param prop_function: a proposition function to apply
:param vars_count: the number of variables for the proposition
"""
args = [truth_gen(vars_count)]
t_count = 0
prop_type = ""
print("p", "q", "r" if vars_count >= 3 else "", prop_function()[1])
for i in truth_gen(vars_count):
print(*i[0:], "{0:>{1}}".format(prop_function(i[:])[0],
len(prop_function()[1]) / 2))
if prop_function(i[:])[0] is 'T':
t_count += 1
f_count = 2 ** vars_count - t_count
if t_count == 2 ** vars_count:
prop_type = "a tautology"
elif t_count == 0:
prop_type = "a contradiction"
else:
prop_type = "neither a Tautology nor a Contradiction"
print("Truths:", t_count, "\n", "\rFalsehoods:", f_count, "\n",
"\rThe proposition is", prop_type, "\n", "_"*20)
# Main code to run all the propositions.
props = [prop1, prop2, prop3, prop4]
for key, prop in enumerate(props):
print("Proposition", key + 1)
apply_prop(prop, (3 if key <= 1 else 2))
View File
Whitespace-only changes.
+12
View File
@@ -0,0 +1,12 @@
domain = list(map(int, input().split(" ")))
a, b, c, d, = True, False, False, True
for x in domain:
if x < 0 and x % 2 is not 0:
a = False
if (x < 0 and x % 2 is 0) or (not (x < 0) and x % 2 is 0):
b = True
if x < 0 and x % 2 is 0:
c = True
if (x - 1) % 3 is not 0 or (x - 1) % 2 is not 0:
d = False
print("\na) is", a, "\nb) is", b, "\nc) is ", c, "\nd) is", d)
+12
View File
@@ -0,0 +1,12 @@
# CS 278 Lab 2
- Programmed in Python3
## How to Run on CS Machines
```
cd <source folder>
python3 lab2.py
```
+31
View File
@@ -0,0 +1,31 @@
def is_prime(n):
if n <= 0 or n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i = i + 6
return True
def hundred_squares():
for number in range(100):
yield number ** 2
def check():
for n in range(3, 10000, 2):
for cnt, square in enumerate(hundred_squares()):
if is_prime((n - (square * 2))):
break
elif cnt >= 99:
print("The smallest one is", n)
return
check()
+11
View File
@@ -0,0 +1,11 @@
# CS 278 Lab 5
- Programmed in Python3
## How to Run on CS Machines
```
cd <source folder>
python3 lab5.py
```
+28
View File
@@ -0,0 +1,28 @@
import itertools
def bin_count(size):
for i in itertools.product([0, 1], repeat=size):
yield i
size_of = int(input("Please enter the size of S: "))
set_A = [int(char) for char in input("Please enter the elements of S: ")]
N = int(input("Please enter the number N: "))
all_subsets = []
for binary in bin_count(len(set_A)):
indices = [i for i, j in enumerate(binary) if j == 1]
subset = []
for index in indices:
subset.append(set_A[index])
all_subsets.append(subset)
count = 0
for cnt, sub in enumerate(all_subsets):
if sum(sub) == N:
tail = ","
count += 1
else:
tail = " "
print(sub if sum(sub) == N else "", end=tail)
print("there are", count, "subsets that sum up to ", N)
View File
Whitespace-only changes.
+11
View File
@@ -0,0 +1,11 @@
# CS 278 Lab 6
Done in python
## How to Run on CS Machines
```
cd <source folder>
python3 lab6.py
```
+70
View File
@@ -0,0 +1,70 @@
Y = list(range(1, int(input("Please enter the value of n: ")) + 1))
def function_generator(targ):
num = 0
for a in targ:
for b in targ:
for c in targ:
num += 1
yield (a, b, c, num)
def numstr(num, x):
return "f" + str(num) + "(" + str(x) + ")="
def is_1to1(a, b, c):
if a != b != c != a:
return True
else:
return False
def is_onto(a, b, c, targ):
output_set = {a, b, c}
targ_set = set(targ)
if len(targ) > 3:
return False
if output_set == targ_set:
return True
else:
return False
def is_biject(a, b, c, targ):
if is_onto(a, b, c, targ) and is_1to1(a, b, c):
return True
else:
return False
def get_truths(a, b, c, targ):
return is_1to1(a, b, c), is_onto(a, b, c, targ), is_biject(a, b, c, targ)
def print_funcs(targ):
count_oto = 0
count_onto = 0
count_bi = 0
for i in function_generator(targ):
a, b, c, num = i
oto, onto, bi = get_truths(a, b, c, targ)
if oto:
count_oto += 1
if onto:
count_onto += 1
if bi:
count_bi += 1
print("=" * 10, "FUNCTION", num, "=" * 10)
print(
numstr(num, "a") + str(a),
numstr(num, "b") + str(b),
numstr(num, "c") + str(c))
print("One-To-One: ", oto)
print("Onto: \t\t", onto)
print("Bijection: ", bi, end="\n\n")
print_funcs(Y)
+11
View File
@@ -0,0 +1,11 @@
# CS 278 Lab 9
Done in python
## How to Run on CS Machines
```
cd <source folder>
python3 lab9.py
```
+23
View File
@@ -0,0 +1,23 @@
def sum1(n):
# base case
if n == 0:
return 2 ** 0
# recursive case
return 2 ** n + sum1(n - 1)
def sum2(n):
# base case
if n == 1:
return 2
# recursive case
return (n * (n + 1)) + (sum2(n - 1))
user_n = int(input("Please enter the value of n: "))
print("The value of the 1st summation is", sum1(user_n))
print("The value of the 2nd summation is", sum2(user_n))
+23
View File
@@ -0,0 +1,23 @@
in_data = input("Please enter 4-digit code: ")
while True:
data_list = [char for char in in_data]
first_digit = 0
second_digit = 0
in_guess = input("Please enter a guess or 0 to stop")
if in_guess == "0":
break
guess_list = [char for char in in_guess]
score1 = 0
for key, val in enumerate(guess_list):
if guess_list[key] == data_list[key]:
guess_list[key] = 0
data_list[key] = 0
score1+=10
for key, val in enumerate(guess_list):
for idx, i in enumerate(data_list):
if i == guess_list[key] and guess_list[key] != 0 and i != 0:
score1+=1
guess_list[key] = 0
data_list[idx] = 0
print("Score: ", score1)
+70
View File
@@ -0,0 +1,70 @@
from turtle import *
rt(90)
rt(-90)
rt(-90)
rt(300)
rt(-300)
fd(300)
rt(-180)
fd(300)
rt(90)
fd(300)
bk(300*(25/100))
rt(90)
bk(300*(50/100))
fd(300*(50/100))
fd(300*(50/100))
bk(300*(50/100))
rt(90)
bk(300*(25/100))
fd(300*(25/100))
fd(300*(25/100))
rt(90)
fd(300*(50/100))
bk(300*(50/100))
rt(90)
rt(90)
rt(90)
fd(300*(50/100))
fd(300*(50/100))
rt(90)
fd(300*(40/100))
rt(90)
fd(300*(23/100))
bk(300*(23/100))
rt(90)
rt(90)
fd(300*(50/100))
bk(300*(50/100))
rt(90)
rt(90)
rt(90)
fd(300*(40/100))
rt(90)
fd(300*(50/100))
bk(300*(23/100))
rt(90)
bk(300*(20/100))
fd(300*(20/100))
rt(90)
fd(300*(20/100))
bk(300*(20/100))
fd(300*(23/100))
bk(300*(23/100))
fd(300*(25/100))
fd(10)
bk(1)
bk(1)
bk(1)
bk(1)
rt(90)
fd(300*(25/100))
fd(300*(5/100))
bk(300*(5/100))
bk(300*(25/100))
rt(-90)
fd(300*(25/100))
fd(300*(25/100))
rt(-90)
fd(300)
done()