Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
88 changes: 88 additions & 0 deletions HW1/hw01_ron.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
""" Homework 1: Control """

# Q1
from operator import add, sub

def a_plus_abs_b(a, b):
if b < 0:
result= float(a+abs(b))
return result
else:
result=float(a+b)
return result



# Q2
def two_of_three(a, b, c):
return x**2 + y**2 + z**2 - min(x, y, z) ** 2

def two_biggest(a, b, c):
if a>=b>=c:
return a**2+b**2
elif b>=c>=a:
return b**2+c**2
else:
return c**2 + a**2
# Q3
def largest_factor(n):
for i in range(n-1, 0, -1):
if n % i == 0:
return i
# Q4
def if_function(condition, true_result, false_result):
if condition:
return true_result
else:
return false_result


def with_if_statement():
"""
>>> result = with_if_statement()
2
>>> print(result)
None
"""
if c():
return t()
else:
return f()

def with_if_function():
"""
>>> result = with_if_function()
1
2
>>> print(result)
None
"""
return if_function(c(), t(), f())

def c():
"*** YOUR CODE HERE ***"

def t():
"*** YOUR CODE HERE ***"

def f():
"*** YOUR CODE HERE ***"

# Q5
def hailstone(n):
count = 1
while n != 1:
if n % 2 == 0:
n = n / 2
else:
n = ((n * 3) + 1)
print(n)
count += 1
print(count)



# Q6
quine = """
"*** YOUR CODE HERE ***"
"""
Binary file added HW2/.DS_Store
Binary file not shown.
93 changes: 93 additions & 0 deletions HW3/hw03_ron.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
HW_SOURCE_FILE = 'hw03.py'

#############
# Questions #
#############

from operator import add, mul, sub

square = lambda x: x * x

identity = lambda x: x

triple = lambda x: 3 * x

increment = lambda x: x + 1


def has_seven(k):
if k % 10 == 7:
return True
elif k == 0:
return False
else:
return has_seven(k // 10)


def pingpong(n):
if n <= 7:
return n
return direction(n) + pingpong(n - 1)


def direction(n):
if n < 7:
return 1
if (n - 1) % 7 == 0 or has_seven(n - 1):
return -1 * direction(n - 1)
return direction(n - 1)


def accumulate(combiner, base, n, term):
"""Return the result of combining the first n terms in a sequence and base.
The terms to be combined are term(1), term(2), ..., term(n). combiner is a
two-argument, associative function.

>>> accumulate(add, 0, 5, identity) # 0 + 1 + 2 + 3 + 4 + 5
15
>>> accumulate(add, 11, 5, identity) # 11 + 1 + 2 + 3 + 4 + 5
26
>>> accumulate(add, 11, 0, identity) # 11
11
>>> accumulate(add, 11, 3, square) # 11 + 1^2 + 2^2 + 3^2
25
>>> accumulate(mul, 2, 3, square) # 2 * 1^2 * 2^2 * 3^2
72
"""
total, k = base, 1
while k <= n:
total, k = combiner(total, term(k)), k + 1
return total

def filtered_accumulate(combiner, base, pred, n, term):
"""Return the result of combining the terms in a sequence of N terms
that satisfy the predicate pred. combiner is a two-argument function.
If v1, v2, ..., vk are the values in term(1), term(2), ..., term(N)
that satisfy pred, then the result is
base combiner v1 combiner v2 ... combiner vk
(treating combiner as if it were a binary operator, like +). The
implementation uses accumulate.

>>> filtered_accumulate(add, 0, lambda x: True, 5, identity) # 0 + 1 + 2 + 3 + 4 + 5
15
>>> filtered_accumulate(add, 11, lambda x: False, 5, identity) # 11
11
>>> filtered_accumulate(add, 0, odd, 5, identity) # 0 + 1 + 3 + 5
9
>>> filtered_accumulate(mul, 1, greater_than_5, 5, square) # 1 * 9 * 16 * 25
3600
>>> # Do not use while/for loops or recursion
>>> from construct_check import check
>>> check(HW_SOURCE_FILE, 'filtered_accumulate',
... ['While', 'For', 'Recursion'])
True
"""
def combine_if(x, y):
"*** YOUR CODE HERE ***"
return accumulate(combine_if, base, n, term)

def odd(x):
return x % 2 == 1

def greater_than_5(x):
return x > 5
115 changes: 115 additions & 0 deletions HW4/ron_hw04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
HW_SOURCE_FILE = 'hw04.py'

###############
# Questions #
###############

def intersection(st, ave):
"""Represent an intersection using the Cantor pairing function."""
return (st+ave)*(st+ave+1)//2 + ave

def street(inter):
return w(inter) - avenue(inter)

def avenue(inter):
return inter - (w(inter) ** 2 + w(inter)) // 2

w = lambda z: int(((8*z+1)**0.5-1)/2)

def taxicab(a, b):
return (max(street(a), street(b)) - min(street(a), street(b)) + max(avenue(a), avenue(b)) - max(avenue(a),avenue(b)))

def squares(s):
"""Returns a new list containing square roots of the elements of the
original list that are perfect squares.

>>> seq = [8, 49, 8, 9, 2, 1, 100, 102]
>>> squares(seq)
[7, 3, 1, 10]
>>> seq = [500, 30]
>>> squares(seq)
[]
"""
"*** YOUR CODE HERE ***"

def g(n):
def g(n):
if n<=3:
return n
else:
return g(n-1)+2*g(n-2)+3*g(n-3)

def g_iter2(n):
i = 1
curr, mid, next = 1, 2, 3
while i < n:
curr, mid, next = mid, next, next + mid * 2 + curr * 3
i += 1
return curr

def count_change(amount):
def largest_demon(n):
n=1
while n<=amount:
if n//2==0:
return n
n*=2

def count_partition(amount, m):
if amount==0 or m==0:
return 1
elif amount<0:
return 0
else:
return count_partition(amount - m, m) + count_partition(amount, m // 2)

def print_move(origin, destination):
"""Print instructions to move a disk."""
print("Move the top disk from rod", origin, "to rod", destination)

def move_stack(n, start, end):
"""Print the moves required to move n disks on the start pole to the end
pole without violating the rules of Towers of Hanoi.

n -- number of disks
start -- a pole position, either 1, 2, or 3
end -- a pole position, either 1, 2, or 3

There are exactly three poles, and start and end must be different. Assume
that the start pole has at least n disks of increasing size, and the end
pole is either empty or has a top disk larger than the top n start disks.

>>> move_stack(1, 1, 3)
Move the top disk from rod 1 to rod 3
>>> move_stack(2, 1, 3)
Move the top disk from rod 1 to rod 2
Move the top disk from rod 1 to rod 3
Move the top disk from rod 2 to rod 3
>>> move_stack(3, 1, 3)
Move the top disk from rod 1 to rod 3
Move the top disk from rod 1 to rod 2
Move the top disk from rod 3 to rod 2
Move the top disk from rod 1 to rod 3
Move the top disk from rod 2 to rod 1
Move the top disk from rod 2 to rod 3
Move the top disk from rod 1 to rod 3
"""
assert 1 <= start <= 3 and 1 <= end <= 3 and start != end, "Bad start/end"
"*** YOUR CODE HERE ***"

###################
# Extra Questions #
###################

from operator import sub, mul

def make_anonymous_factorial():
"""Return the value of an expression that computes factorial.

>>> make_anonymous_factorial()(5)
120
>>> from construct_check import check
>>> check(HW_SOURCE_FILE, 'make_anonymous_factorial', ['Assign', 'AugAssign', 'FunctionDef', 'Recursion'])
True
"""
return 'YOUR_EXPRESSION_HERE'
Loading