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
9 changed files
with
313 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,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,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,6 @@ | ||
``` | ||
Karan Chawla | ||
Challenge 14 | ||
``` | ||
|
||
Approach: The aim here is to reverse a linked list. This is achieved by using three pointers - prev, current and next. Current moves along the linked list while the prev and next pointers are switched. Finally the head is set to the prev - completing the list reversal. |
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,88 @@ | ||
//Karan Chawla | ||
//Challenge 14 | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
//Linked list node definition | ||
typedef struct node | ||
{ | ||
char c; | ||
struct node* next; | ||
}Node; | ||
|
||
|
||
//utility function to create a new node | ||
Node* newNode(char c) | ||
{ | ||
Node* new_node = (Node*)malloc(sizeof(Node)); | ||
new_node->c = c; | ||
new_node->next = NULL; | ||
|
||
return new_node; | ||
} | ||
|
||
//utility function to add a new node to the linked list | ||
void push(Node** head_ref, char c) | ||
{ | ||
Node* new_node = newNode(c); | ||
|
||
new_node->next = *head_ref; | ||
|
||
*head_ref = new_node; | ||
} | ||
|
||
|
||
//function to reverse the linked list | ||
//prev->curr->next | ||
//changes prev to next and moves current along the linked list until NULL | ||
void reverseLinkedList(Node** head_ref) | ||
{ | ||
if (*head_ref==NULL) | ||
return; | ||
|
||
Node* prev = NULL; | ||
Node* current = *head_ref; | ||
Node* next; | ||
while(current!=NULL) | ||
{ | ||
next = current->next; | ||
current->next = prev; | ||
prev = current; | ||
current = next; | ||
} | ||
*head_ref = prev; | ||
} | ||
|
||
//utility function to print linked list | ||
void printLinkedList(Node* head) | ||
{ | ||
while(head!=NULL) | ||
{ | ||
printf("%c->", head->c); | ||
head = head->next; | ||
} | ||
printf("NULL\n"); | ||
} | ||
|
||
//driver program | ||
int main(void) | ||
{ | ||
Node* head = NULL; | ||
//create a linked list with chars from "murder" | ||
push(&head,'r'); | ||
push(&head,'e'); | ||
push(&head,'d'); | ||
push(&head,'r'); | ||
push(&head,'u'); | ||
push(&head,'m'); | ||
|
||
//print the linked list | ||
printLinkedList(head); | ||
//reverse the linked list | ||
reverseLinkedList(&head); | ||
//print the reversed linked list | ||
printLinkedList(head); | ||
|
||
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,23 @@ | ||
def builtInMethod(inputList): | ||
for i in xrange(len(inputList)): | ||
# Check if i is in list | ||
if i not in inputList: | ||
# Return number | ||
return i | ||
# Return 0 if none of the numbers meet the condition | ||
return 0 | ||
|
||
|
||
def sumMethod(inputList): | ||
# find (n*n+1)/2 | ||
fullSum = (len(inputList) * (len(inputList) + 1)) / 2 | ||
# subtract sum of full range from supplied range | ||
return fullSum - sum(inputList) | ||
|
||
|
||
def main(): | ||
pass | ||
|
||
|
||
if __name__ == '__main__': | ||
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,23 @@ | ||
# Challenge 7 - Find the Missing Number | ||
|
||
*Python Version:* 2.7 | ||
|
||
In a list 0..N, find which digit is missing. | ||
|
||
# Functions | ||
|
||
## builtInMethod | ||
|
||
**Input:** List | ||
**Output:** Integer | ||
|
||
Finds the integer 0 < x < N not present in list. The 'not in' syntax performs at O(N) according to [the Python Wiki](https://wiki.python.org/moin/TimeComplexity), so that is used to check the list for the value in the range. | ||
|
||
**UPDATE**: 'not in' syntax is O(N), but the fact that it's in another loop makes the solution O(N^2). Thanks erocs. | ||
|
||
## sumMethod | ||
|
||
**Input:** List | ||
**Output:** Integer | ||
|
||
Uses the principle of sequential ranges (n*n+1)/2 to find the missing element. The sum of the input list is subtracted from the sum of the full range, exposing the missing element. |
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,108 @@ | ||
import MissingNumber | ||
import unittest | ||
|
||
|
||
class BuiltInTests(unittest.TestCase): | ||
"""tests for builtInMethod""" | ||
|
||
def testMissing3(self): | ||
self.assertEqual(MissingNumber. | ||
builtInMethod([0, 1, 2, 4]), 3) | ||
|
||
def testMissing2(self): | ||
self.assertEqual(MissingNumber. | ||
builtInMethod([1, 3, 4, 0]), 2) | ||
|
||
def testNoMissingNumber(self): | ||
self.assertEqual(MissingNumber. | ||
builtInMethod([1, 2, 3]), 0) | ||
|
||
def testLongerList(self): | ||
self.assertEqual(MissingNumber. | ||
builtInMethod([0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, | ||
12, 13, 14, 15, 16, 17, 18, 19, | ||
20, 21, 22, 23, 24]), 7) | ||
|
||
def testEvenLongerRandomList(self): | ||
self.assertEqual(MissingNumber. | ||
builtInMethod([78, 53, 118, 99, 154, 5, 52, 184, | ||
73, 17, 92, 107, 34, 159, 101, 30, | ||
171, 95, 100, 54, 63, 169, 37, 45, | ||
174, 158, 57, 0, 89, 61, 71, 188, | ||
83, 18, 133, 149, 62, 151, 90, 3, | ||
141, 126, 165, 179, 103, 166, 68, | ||
177, 163, 190, 77, 55, 31, 187, 16, | ||
91, 47, 93, 146, 147, 74, 185, 135, | ||
102, 41, 125, 164, 12, 19, 168, 96, | ||
23, 86, 194, 9, 132, 104, 122, 24, | ||
144, 161, 117, 183, 196, 176, 155, | ||
108, 172, 120, 160, 182, 50, 75, 22, | ||
15, 49, 80, 127, 106, 20, 148, 139, | ||
38, 134, 67, 123, 56, 88, 40, 186, | ||
131, 76, 59, 21, 43, 4, 14, 115, | ||
162, 199, 48, 128, 81, 39, 11, 97, | ||
137, 153, 10, 65, 138, 114, 79, 6, | ||
195, 156, 180, 8, 28, 60, 192, 42, | ||
119, 64, 2, 200, 116, 129, 51, 178, | ||
191, 121, 82, 143, 26, 110, 33, 136, | ||
32, 175, 27, 150, 111, 105, 70, 157, | ||
44, 13, 46, 170, 58, 69, 198, 193, | ||
167, 25, 29, 181, 66, 72, 36, 87, 7, | ||
98, 130, 152, 94, 189, 1, 84, 109, | ||
113, 197, 145, 35, 124, 112, 173, | ||
140, 142]), 85) | ||
|
||
|
||
class SumMethodTests(unittest.TestCase): | ||
"""tests for sumMethod""" | ||
|
||
def testMissing3(self): | ||
self.assertEqual(MissingNumber. | ||
sumMethod([0, 1, 2, 4]), 3) | ||
|
||
def testMissing2(self): | ||
self.assertEqual(MissingNumber. | ||
sumMethod([1, 3, 4, 0]), 2) | ||
|
||
def testNoMissingNumber(self): | ||
self.assertEqual(MissingNumber. | ||
sumMethod([1, 2, 3]), 0) | ||
|
||
def testLongerList(self): | ||
self.assertEqual(MissingNumber. | ||
sumMethod([0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, | ||
12, 13, 14, 15, 16, 17, 18, 19, | ||
20, 21, 22, 23, 24]), 7) | ||
|
||
def testEvenLongerRandomList(self): | ||
self.assertEqual(MissingNumber. | ||
sumMethod([78, 53, 118, 99, 154, 5, 52, 184, | ||
73, 17, 92, 107, 34, 159, 101, 30, | ||
171, 95, 100, 54, 63, 169, 37, 45, | ||
174, 158, 57, 0, 89, 61, 71, 188, | ||
83, 18, 133, 149, 62, 151, 90, 3, | ||
141, 126, 165, 179, 103, 166, 68, | ||
177, 163, 190, 77, 55, 31, 187, 16, | ||
91, 47, 93, 146, 147, 74, 185, 135, | ||
102, 41, 125, 164, 12, 19, 168, 96, | ||
23, 86, 194, 9, 132, 104, 122, 24, | ||
144, 161, 117, 183, 196, 176, 155, | ||
108, 172, 120, 160, 182, 50, 75, 22, | ||
15, 49, 80, 127, 106, 20, 148, 139, | ||
38, 134, 67, 123, 56, 88, 40, 186, | ||
131, 76, 59, 21, 43, 4, 14, 115, | ||
162, 199, 48, 128, 81, 39, 11, 97, | ||
137, 153, 10, 65, 138, 114, 79, 6, | ||
195, 156, 180, 8, 28, 60, 192, 42, | ||
119, 64, 2, 200, 116, 129, 51, 178, | ||
191, 121, 82, 143, 26, 110, 33, 136, | ||
32, 175, 27, 150, 111, 105, 70, 157, | ||
44, 13, 46, 170, 58, 69, 198, 193, | ||
167, 25, 29, 181, 66, 72, 36, 87, 7, | ||
98, 130, 152, 94, 189, 1, 84, 109, | ||
113, 197, 145, 35, 124, 112, 173, | ||
140, 142]), 85) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |