forked from YearOfProgramming/2017Challenges
-
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.
Merge pull request YearOfProgramming#141 from Flickdm/master
Challenge 0 - Python, Challenge 1 - CPP, Challenge 2 - Python
- Loading branch information
Showing
7 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
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,7 @@ | ||
# Challenge_0 | ||
|
||
Simple Hello_world | ||
|
||
I import print_function because in Python2 print was a statement, whereas | ||
starting in python3 print became a function. This line allows for use in both | ||
versions of python. |
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,9 @@ | ||
# | ||
# File: hello_world.py | ||
# Author: Douglas Flick | ||
# 2017Challenge_0 | ||
|
||
#import print_function for python2.7 support | ||
from __future__ import print_function | ||
|
||
print("Hello World!") |
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,4 @@ | ||
# Challenge 1 | ||
|
||
We interate over half the length of the string and swap the characters for start | ||
and end and move inwards. |
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,41 @@ | ||
#include <iostream> | ||
#include <string.h> | ||
|
||
|
||
// Prototypes | ||
std::string reverseString(std::string myString); | ||
void swap(char &a, char &b); | ||
|
||
int main(int argc, char* argv[]) { | ||
|
||
// define our input string | ||
std::string inputString; | ||
|
||
// use stdin to grab our string | ||
// *Note using getline will allow us to grab a full sentence | ||
std::getline(std::cin, inputString); | ||
//output our reversal | ||
std::cout << reverseString(inputString) << '\n'; | ||
|
||
return 0; | ||
} | ||
|
||
//swap function by reference | ||
void swap(char &a, char &b) { | ||
char tmp = a; | ||
a = b; | ||
b = tmp; | ||
} | ||
|
||
std::string reverseString(std::string inputStr) { | ||
//Grab length | ||
unsigned int inputStrLen = inputStr.length(); | ||
|
||
//Iterate over half of the length | ||
for(int i=0; i < inputStrLen/2; i++) { | ||
//swap first and last and loop | ||
swap(inputStr[i], inputStr[inputStrLen-1-i]); | ||
} | ||
|
||
return inputStr; | ||
} |
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,4 @@ | ||
#Challenge_2 | ||
|
||
|
||
The code is mostly self documenting |
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,26 @@ | ||
# should work with both python2 and python3 | ||
|
||
def findSingleton(sList): | ||
""" Returns entries that only occured once in list """ | ||
|
||
# Count dict is used for storing if we've seen a element | ||
# and how many times it occured | ||
count_dict = {} | ||
# Single dict is used for storing which elements we've seen only once | ||
single_dict = {} | ||
|
||
# Iterate over every key in our list | ||
for key in sList: | ||
# If the key exists in our count dict | ||
if key in count_dict.keys(): | ||
# count up | ||
count_dict[key] += 1 | ||
# remove element from single dict | ||
single_dict.pop(key, None) | ||
else: | ||
#initiate key in both dicts | ||
count_dict[key] = 1 | ||
single_dict[key] = True | ||
|
||
# return the keys in dict | ||
return single_dict.keys() |
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,26 @@ | ||
import unittest | ||
|
||
from challenge2 import findSingleton | ||
|
||
class TestFindSingleton(unittest.TestCase): | ||
def test_base_case(self): | ||
aList = [2, 3, 4, 2, 3, 5, 4, 6, 4, 6, 9, 10, 9, 8, 7, 8, 10, 7] | ||
bList = [2,'a', 'l', 3, 'l', 4, 'k', 2, 3, 4, 5, 'a', 6, 'c', 4, 'm', 6, 'm', 'k', 9, 10, 9, 8, 7, 8, 10, 7] | ||
|
||
self.assertEqual(findSingleton(aList), [5]) | ||
self.assertEqual(findSingleton(bList), ['c', 5]) | ||
|
||
def test_empty(self): | ||
aList = [] | ||
bList = [1, 1, 1] | ||
|
||
self.assertEqual(findSingleton(aList), []) | ||
self.assertEqual(findSingleton(bList), []) | ||
|
||
def test_similiar_ascii(self): | ||
aList = [73, 'I'] | ||
|
||
self.assertEqual(findSingleton(aList), ['I', 73]) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |