I am an experienced programmer, with C/C++, Java and Python experience.
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
|
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 3, HW 1
LIMIT = 200
def is_diophantine_num(n, pack_sizes):
'''
It is diophantine number if we can find non-negative numbers
a, b and c such that:
p1 * a + p2 * b + p3 * c = n.
'''
p1, p2, p3 = pack_sizes
options = range(int(n / p1) + 1)
result = [(a, b, c) for a in options for b in options for c in options
if p1 * a + p2 * b + p3 * c == n]
#print "a,b,c for {0}: {1}".format(n, result)
return bool(result)
def find_largest_nondiophantine_num(pack_sizes):
smallest_possible_diophantine = pack_sizes[0]
largest_nondiophantine = smallest_possible_diophantine - 1
count_diophantine = 0
for n in xrange(smallest_possible_diophantine, LIMIT):
if is_diophantine_num(n, pack_sizes):
count_diophantine += 1
if count_diophantine == smallest_possible_diophantine:
return largest_nondiophantine
else:
count_diophantine = 0
largest_nondiophantine = n
if name == 'main':
pack_sizes = (6, 9, 20)
print "largest non diophantine num for pack sizes {0} is {1}".format(
pack_sizes, find_largest_nondiophantine_num(pack_sizes))
rohshall
7 months ago
|
 |
Bash Scripting: Lesson 5, HW 1
/usr/bin/env sh
re="([a-zA-Z ]) - ([0-9]) - (.*)$"
while read cdtrack; do
if [[ "$cdtrack" =~ $re ]]; then
echo Track ${BASH_REMATCH[2]} is ${BASH_REMATCH[3]}
fi
done < tracks.txt
The cookbook does not say this because it predates the version 3.2, but using quotes in RHS in =~ makes it a string comparison rather than a regex match. That is why RE needs to be stored in a variable first.
rohshall
8 months ago
|
 |
|
 |
|
 |
Programming in C: Lesson 1, HW 1
include
int main() {
printf("Hello Worldc");
return 0;
}
I got:
assignment.c: In function ‘main’:
assignment.c:4:10: warning: unknown escape sequence: 'c'
rohshall
8 months ago
|
 |
Bash Scripting: Lesson 2, HW 1
/usr/bin/env sh
function choose {
read -p "Would you give me your password? (y/n): " answer
case $answer in
[yY] ) echo "you are a darling"
read -s -p "password: " password
printf "n"
;;
[nN] ) echo "You are angry with me"
;;
* ) echo "Hmm...."
;;
esac
}
choose
if [ -n "$password" ]; then
echo "Your password is:" $password
fi
rohshall
8 months ago
|
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1
import math
import itertools
def isPrime(num):
limit = int(math.sqrt(num))
for i in xrange(2, limit+1):
if num%i == 0:
return False
return True
def primes():
yield 2
num = 3
while True:
if isPrime(num):
yield num
num += 2
print 1000th number
thousandth = next(itertools.islice(primes(), 999, 1000))
print thousandth
def primes_with_sum_of_logs():
yield 2, 0
num = 3
sum_of_logs = 0
while True:
if isPrime(num):
yield num, sum_of_logs
sum_of_logs += math.log(num)
num += 2
for num, sum_of_logs in itertools.islice(primes_with_sum_of_logs(), 1000):
print num, sum_of_logs, sum_of_logs / num
rohshall
8 months ago
|
 |
|
 |
|