-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
the adjective verb noun goes to town. |