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 branch 'master' of https://github.com/YearOfProgramming/2017Cha…
- Loading branch information
Showing
54 changed files
with
1,583 additions
and
14 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,14 @@ | ||
# Challenge 0 - Hello World | ||
###### C# | ||
|
||
### 1. Approach to Solving the problem | ||
|
||
Output text "Hello World" | ||
|
||
### 2. How to compile and run this code | ||
|
||
From the /src directory, use `csc.exe hello.cs` | ||
|
||
### 3. How this program works | ||
|
||
Read the text! Hi :) |
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,13 @@ | ||
using System; | ||
namespace HelloWorld | ||
{ | ||
class Hello | ||
{ | ||
static void Main() | ||
{ | ||
Console.WriteLine("Hello World!"); | ||
Console.WriteLine("Press any key to exit."); | ||
Console.ReadKey(); | ||
} | ||
} | ||
} |
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,16 @@ | ||
/** | ||
* This class holds the code needed to meet the requirements for Challenge 0 of | ||
* the 2017 Year of Programming. | ||
* | ||
* @author Jake Bashaw | ||
*/ | ||
public class Challenge_0 | ||
{ | ||
/** | ||
* Prints "Hello World" for the user to view | ||
*/ | ||
public static void main(String args[]) | ||
{ | ||
System.out.println("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 @@ | ||
String reversing in C |
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,33 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
char *readLine() { | ||
int length = 1; | ||
char *string = NULL; | ||
char c; | ||
while (EOF!=(c=getchar()) && '\r'!=c && '\n' != c) { | ||
string=realloc(string,length); | ||
string[length-1]=c; | ||
length++; | ||
} | ||
string[length-1]='\0'; | ||
return string; | ||
} | ||
int main(int argc, char **argv) { | ||
char *input = readLine(); | ||
char temp; | ||
int length=0, i=0; | ||
for (temp=input[length]; temp!='\0'; length++) temp=input[length]; | ||
length--; | ||
char *output = (char*) malloc(sizeof(char)*(length+1)); | ||
if(length==0) { | ||
output[0]='\0'; | ||
} | ||
else { | ||
for (i=0; i<length; i++) { | ||
output[i]=input[length-i-1]; | ||
} | ||
output[i]='\0'; | ||
} | ||
printf("%s\n", output); | ||
} |
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,38 @@ | ||
# Challenge_1 | ||
###### C compiled with gcc 4.2.1 | ||
|
||
### 1. Approch to Solving the problem | ||
|
||
Reversing strings in C is a little bit more challenging than | ||
higher level languages. Since basically everything is a pointer | ||
to memory of a particular size, you must make sure that the variables | ||
you're creating have the approprate space. In short, I created two | ||
character arrays and looped through them from opposite directions, | ||
the original's end filling the beginning of the reverse string. | ||
|
||
### 2. How to compile and run this code | ||
|
||
Make sure that you have gcc installed on your *nix machine. To see if you have | ||
it installed you can type: | ||
|
||
``` | ||
$ gcc --version | ||
``` | ||
|
||
Compiling: | ||
``` | ||
$ gcc -o challenge1 challenge1.c | ||
``` | ||
Or optionally with symbols that aid in debugging. | ||
``` | ||
$ gcc -g -o challenge0 challenge0.c | ||
``` | ||
|
||
### 3. How this program works | ||
|
||
After the program has compiled with no errors: | ||
``` | ||
$ ./challenge1 | ||
olleH | ||
``` |
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,28 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> // for memset | ||
|
||
int main() | ||
{ | ||
// Initialize a character array with the string you want to reverse. | ||
char helloString[] = "Hello"; | ||
// Get length of 0-indexed string, this will be used for index looping | ||
int strSize = strlen(helloString); | ||
// Instantiated character array equal to the size of the original | ||
char revHelloStr[strSize]; | ||
// Set the values of the reverse character array to zero, this takes care of the null terminator at the end | ||
memset(revHelloStr, 0, strSize*sizeof(char)); | ||
// subtracted one to disclude the null terminator. That is taken care of in revHelloStr by the memset | ||
int i = strSize-1; | ||
int j = 0; | ||
// Perform a count from the front to back of the character array for the reverse string | ||
// while counting from the back of the hello string, skipping the null terminator | ||
for (i,j ; i>=0 && j<=strSize-1 ; i--, j++) | ||
{ | ||
revHelloStr[j] = helloString[i]; | ||
|
||
} | ||
printf("%s",revHelloStr); | ||
|
||
return 0; | ||
} |
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 @@ | ||
# Challenge 1 - Reverse a String | ||
###### C# | ||
|
||
### 1. Approach to Solving the problem | ||
|
||
I had to work out how to get a string, then make it backwards. Turns out there are inbuilt functions for this. | ||
|
||
### 2. How to compile and run this code | ||
|
||
From the /src directory, use `csc.exe program.cs` | ||
|
||
### 3. How this program works | ||
|
||
Takes a string from the console input | ||
Converts to a char array | ||
Uses the 'System.Reverse' function to change the order of the characters | ||
Displays the reversed string |
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,21 @@ | ||
using System; | ||
namespace YOP2017 | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("Enter a string to reverse: "); | ||
string input = Console.ReadLine(); | ||
Console.WriteLine("Your string in reverse: " + Program.reverseString(input)); | ||
Console.WriteLine("Press any key to exit.") | ||
Console.ReadKey(true); | ||
} | ||
public static string reverseString(string input) | ||
{ | ||
char[] arr = input.ToCharArray(); | ||
Array.Reverse(arr); | ||
return new string(arr); | ||
} | ||
} | ||
} |
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 @@ | ||
import java.util.Scanner; | ||
|
||
/** | ||
* This class holds the code needed to meet the requirements for Challenge 1 of | ||
* the 2017 Year of Programming. | ||
* | ||
* @author Jake Bashaw | ||
*/ | ||
public class Challenge_1 | ||
{ | ||
/** | ||
* Prints the reversed version of the standard input supplied by the user | ||
*/ | ||
public static void main(String args[]) | ||
{ | ||
System.out.print("> "); | ||
Scanner kb = new Scanner(System.in); | ||
String input = kb.nextLine(); | ||
System.out.println(reverseString(input)); | ||
} | ||
|
||
/** | ||
* Reverses and returns a String | ||
* | ||
* @param input The String to be reversed | ||
*/ | ||
public static String reverseString(String input) | ||
{ | ||
StringBuilder sb = new StringBuilder(input); | ||
char forChar; | ||
char revChar; | ||
for(int i=0; i<sb.length()/2; i++) | ||
{ | ||
forChar = sb.charAt(i); | ||
revChar = sb.charAt(sb.length()-i-1); | ||
sb.setCharAt(i, revChar); | ||
sb.setCharAt(sb.length()-i-1, forChar); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
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,24 @@ | ||
# Valid Closers | ||
###### Language Version (Python 3.6.0) | ||
|
||
|
||
For this challenge we implement a list as a stack. As we iterate through the | ||
string input we add every opener found into the stack sequentially. | ||
|
||
As the iteration continues onto the closers, check the if closer matches the last | ||
element in our stack. If match is successful, pop the element off the stack and continue. | ||
If any pair fails to match then the string input contains an invalid closer and | ||
the function returns False. | ||
|
||
After iteration is complete if our stack is empty then every opener had a valid | ||
closer and the function returns True. | ||
|
||
|
||
Run ```test.py``` to test code against the unit test. | ||
``` | ||
........ | ||
---------------------------------------------------------------------- | ||
Ran 8 tests in 0.001s | ||
OK | ||
``` |
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,35 @@ | ||
# Valid Closers | ||
|
||
def is_valid_closer(string): | ||
""" | ||
Returns True if every opener has a valid closer | ||
""" | ||
openers = ['[', '(', '{'] | ||
closers = [']', ')', '}'] | ||
stack = [] | ||
|
||
for ch in string: | ||
# If there is a closer, but no openers | ||
if not stack and ch in closers: | ||
return False | ||
|
||
# Add any opener into the stack | ||
elif ch in openers: | ||
stack.append(ch) | ||
|
||
# If the following closers do not pair with the opener popped from stack | ||
# then the function will return False | ||
elif ch == ']': | ||
if stack.pop() != '[': | ||
return False | ||
|
||
elif ch == ')': | ||
if stack.pop() != '(': | ||
return False | ||
|
||
elif ch == '}': | ||
if stack.pop() != '{': | ||
return False | ||
|
||
# Nothing in our stack == True | ||
return not stack |
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,33 @@ | ||
import unittest | ||
|
||
from closers import is_valid_closer | ||
|
||
class Test(unittest.TestCase): | ||
|
||
def test1(self): | ||
self.assertEqual(is_valid_closer('{{{{{{{{{adfkjaefia}}}}}}}'), False) | ||
|
||
def test2(self): | ||
self.assertEqual(is_valid_closer('{{{{{{{{{[[[[[[kadfa{{{{{{{((({daljfdaf({{{[]}}kaldjfs})})))}}}}}}}]]]]]]}kjfela}}}}}}}}'), True) | ||
|
||
def test3(self): | ||
self.assertEqual(is_valid_closer('{{{[}}}}dafda'), False) | ||
|
||
def test4(self): | ||
self.assertEqual(is_valid_closer('{{{{{{{{{}}}}}}}}}'), True) | ||
|
||
def test5(self): | ||
self.assertEqual(is_valid_closer('[[[[[[[[[kafjalfeianfailfeja;fjai;efa;sfj]]]]]]]]]kjajdain'), True) | ||
|
||
def test6(self): | ||
self.assertEqual(is_valid_closer(''), True) | ||
|
||
def test7(self): | ||
self.assertEqual(is_valid_closer('((((((fjdalfeja((((alefjalisj(())))))))))))d'), True) | ||
|
||
def test8(self): | ||
self.assertEqual(is_valid_closer(')))(((d'), False) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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,5 @@ | ||
{ | ||
"python.linting.pylintEnabled": true, | ||
"python.pythonPath": "python3", | ||
"python.formatting.provider": "autopep8" | ||
} |
Oops, something went wrong.