Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dewie102 committed Jan 21, 2017
2 parents e8ce4c4 + af73be1 commit 45311a3
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 0 deletions.
14 changes: 14 additions & 0 deletions challenge_0/csharp/jfrancis2/README.md
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 :)
13 changes: 13 additions & 0 deletions challenge_0/csharp/jfrancis2/src/hello.cs
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();
}
}
}
17 changes: 17 additions & 0 deletions challenge_1/csharp/jfrancis2/README.md
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
21 changes: 21 additions & 0 deletions challenge_1/csharp/jfrancis2/src/program.cs
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);
}
}
}
6 changes: 6 additions & 0 deletions challenge_14/c/karanchawla/README.md
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.
88 changes: 88 additions & 0 deletions challenge_14/c/karanchawla/challenge_14.c
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;
}
23 changes: 23 additions & 0 deletions challenge_7/python/ajschrier/MissingNumber.py
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()
23 changes: 23 additions & 0 deletions challenge_7/python/ajschrier/README.md
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.
108 changes: 108 additions & 0 deletions challenge_7/python/ajschrier/Test_MissingNumber.py
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()

0 comments on commit 45311a3

Please sign in to comment.