adding cs142 code
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
CXXFLAGS=-Wall -g -std=c++0x
|
||||
all: number
|
||||
clean:
|
||||
rm -vf number
|
||||
|
||||
test: all
|
||||
./number
|
||||
@@ -0,0 +1,53 @@
|
||||
import random
|
||||
import time
|
||||
|
||||
|
||||
def get_numbers(pow2, compliment):
|
||||
numbers = []
|
||||
constant = compliment
|
||||
for i in (0, 1):
|
||||
for j in (0, 1):
|
||||
for k in (0, 1):
|
||||
for l in (0, 1):
|
||||
if pow2 == 0:
|
||||
cur_digits = (i, j, k, l, constant)
|
||||
if pow2 == 1:
|
||||
cur_digits = (i, j, k, constant, l)
|
||||
if pow2 == 2:
|
||||
cur_digits = (i, j, constant, k, l)
|
||||
if pow2 == 3:
|
||||
cur_digits = (i, constant, j, k, l)
|
||||
if pow2 == 4:
|
||||
cur_digits = (constant, i, j, k, l)
|
||||
numbers.append(cur_digits)
|
||||
return numbers
|
||||
|
||||
print "example of how to use randomness to determine compliment"
|
||||
a_few_tries = xrange(3)
|
||||
for i in a_few_tries:
|
||||
compliment = random.randint(0, 3026) % 2
|
||||
for i in get_numbers(pow2=0, compliment=compliment):
|
||||
print i
|
||||
|
||||
print "for loop to print out all numbers from 2^0 to 2^5"
|
||||
for i in (0, 1):
|
||||
for j in (0, 1):
|
||||
for k in (0, 1):
|
||||
for l in (0, 1):
|
||||
for m in (0, 1):
|
||||
for n in (0, 1):
|
||||
print i, j, k, l, m
|
||||
|
||||
print "example function to convert a string in binary to integers: (see source)"
|
||||
def bin2int(s):
|
||||
total = 0
|
||||
for i in xrange(len(s)):
|
||||
if s[i] == '1':
|
||||
total += 2**(len(s) - i - 1)
|
||||
return total
|
||||
|
||||
|
||||
print "here I test out the bin2int func defined above:"
|
||||
test_cases = ('1010', '1011', '0100', '1000', '0111')
|
||||
for test in test_cases:
|
||||
print test, bin2int(test), int(test, 2)
|
||||
@@ -0,0 +1,111 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
#include <time.h>
|
||||
#include <sstream>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int bit2dec(string s);
|
||||
|
||||
vector<int> get_numbers(int pow, int compliment) {
|
||||
string store;
|
||||
vector<int> numbers;
|
||||
int constant = compliment;
|
||||
for(int i = 0; i < 2; i++){
|
||||
for(int j = 0; j < 2; j++){
|
||||
for(int k = 0; k < 2; k++){
|
||||
for(int l = 0; l < 2; l++){
|
||||
ostringstream stream;
|
||||
if(pow == 0){
|
||||
stream << i << j << k << l << constant;
|
||||
}
|
||||
else if(pow == 1){
|
||||
stream << i << j << k << constant << l;
|
||||
}
|
||||
else if(pow == 2){
|
||||
stream << i << j << constant << k << l;
|
||||
}
|
||||
else if(pow == 3){
|
||||
stream << i << constant << j << k << l;
|
||||
}
|
||||
else if(pow == 4){
|
||||
stream << constant << i << j << k << l;
|
||||
}
|
||||
store = stream.str();
|
||||
numbers.push_back(bit2dec(store));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return numbers;
|
||||
}
|
||||
|
||||
|
||||
ostream & operator<<(ostream & os, const vector<int> & nums) {
|
||||
for(unsigned int i = 0; i < nums.size(); i++){
|
||||
if(i % 4 == 0){
|
||||
os << endl;
|
||||
}
|
||||
os << nums[i] << " ";
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
int bit2dec(string s) {
|
||||
|
||||
int total = 0;
|
||||
int power = 0;
|
||||
for(int i = s.size() - 1; i >= 0; i--) {
|
||||
if(s[i] == '1') {
|
||||
total += pow(2,power);
|
||||
}
|
||||
power++;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
srand (time(NULL));
|
||||
int keep_playing = 0;
|
||||
while(keep_playing == 0){
|
||||
ostringstream answer;
|
||||
for(unsigned int i = 0; i < 5 ; i++) {
|
||||
int switcher = rand() % 2;
|
||||
string check;
|
||||
cout << "Is your number in this list? enter y for yes n for no" << endl;
|
||||
cout << get_numbers(i, switcher) << endl;
|
||||
cin >> check;
|
||||
if(switcher == 0){
|
||||
if(check == "y") {
|
||||
answer << '0';
|
||||
}
|
||||
else {
|
||||
answer << '1';
|
||||
}
|
||||
}
|
||||
if(switcher == 1) {
|
||||
if(check == "y") {
|
||||
answer << '1';
|
||||
}
|
||||
else {
|
||||
answer << '0';
|
||||
}
|
||||
}
|
||||
}
|
||||
string final = answer.str();
|
||||
reverse (final.begin(), final.end());
|
||||
cout << bit2dec(final) << endl;
|
||||
cout << "Do you want to keep playing? enter y for yes, n for no" << endl;
|
||||
string check;
|
||||
cin >> check;
|
||||
if(check == "n"){
|
||||
keep_playing++;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
|
||||
vector<int> numbers;
|
||||
|
||||
numbers.push_back(0);
|
||||
numbers.push_back(1);
|
||||
numbers.push_back(0);
|
||||
numbers.push_back(1);
|
||||
numbers.push_back(0);
|
||||
|
||||
cout << numbers[0] << numbers[1]<< numbers[2]<<numbers[3] << numbers[4] <<endl;
|
||||
}
|
||||
Reference in New Issue
Block a user