Skip to content

Commit

Permalink
lab7
Browse files Browse the repository at this point in the history
  • Loading branch information
gigatexal committed May 11, 2016
1 parent 8425928 commit ae6cfa3
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
Binary file added CS161/lab7/a.out
Binary file not shown.
85 changes: 85 additions & 0 deletions CS161/lab7/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/***************************************************
* Name: Alex Narayan
* Date: 5.10.2016
* Assignment: Lab 7
* Class: CS161
* Description: implement a function to raise a base
* to a power given two inputs base and power
* Resources: none
***************************************************/

#include <iostream>

using namespace std;

double pow(int base, int exponent);

void greeting();
void result(int base, int exponent);

int main() {
int base, exponent;
greeting();
cin >> base >> exponent;
result(base,exponent);

system("pause");

return 0;

}

void greeting(){
std::cout << "Please enter two numbers: ";
}

void result(int base, int exponent){
std::cout << "The result of "
<< base
<< " ^ "
<< exponent
<< " is "
<< pow(base,exponent)
<< endl;
}

double pow(int base, int exponent){
//purposefully not using cmath.h for abs and pow functions
// edge cases
// base = 2, exponent = -2, answer = 1/2^2
// base = 2, exponent = 2, answer = 2^2
// base = 2, exponent = 0, answer = 1
// base = 0, exponent = 2, answer = 0
// base = 0, exponent = 0, answer = -1 or undefined
// limitation: infinite loop if user enters float for base or exponent
int result = 1;
if (exponent < 0){
for (int i = exponent * -1; i > 0; i--){
result *= base;
}
return (1.0/(result));
}
if (exponent > 0) {
for (int i = exponent; i > 0; i--){
result *= base;
}
return result;
}

if (base == 0 || exponent == 0){
if (base == 0){
return 0;
}
else if (exponent == 0){
return 1;
}
}


}






14 changes: 14 additions & 0 deletions teachingpython/fib/madlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
verbs = 0;
adjectives = 0;
nouns = 0;

with open('story.txt') as story:
for words in story:
for word in words.split():
words_list = ['verb','adjective','noun'];
if 'verb' in word_list: verbs+=1
if "adjective" in words_list: adjectives+=1;
if "nouns" in words_list_verbs: nouns+1


print (verbs,adjectives,nouns)
17 changes: 17 additions & 0 deletions teachingpython/fib/memo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def memo(func):
cache = {}
def helper(*args):
if args in cache:
return cache[args]
else:
cache[args] = func(*args)
return cache[args]
return helper


@memo
def fib(n):
return n if n < 2 else fib(n-2) + fib(n-1)


print (fib(500))
1 change: 1 addition & 0 deletions teachingpython/fib/story.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
the adjective verb noun goes to town.

0 comments on commit ae6cfa3

Please sign in to comment.