forked from mre/the-coding-interview
-
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.
added new C programs, moved misplaced files
- Loading branch information
1 parent
336e879
commit 2d355ed
Showing
6 changed files
with
156 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,32 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#define MAX_LEN 100 | ||
|
||
int main() | ||
{ | ||
int x, y; | ||
int loops = 0; | ||
char s[MAX_LEN]; | ||
char *p; | ||
printf("Enter number to count loops: "); | ||
scanf("%d", &x); | ||
y = x; | ||
|
||
sprintf(s, "%d", x); | ||
p = s; | ||
while (*p != '\0') | ||
{ | ||
if (*p == '6') | ||
loops++; | ||
else if (*p == '8') | ||
loops += 2; | ||
else if (*p == '9') | ||
loops++; | ||
else if (*p == '0') | ||
loops++; | ||
p++; | ||
} | ||
printf("%d has %d loops\n", y, loops); | ||
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,79 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
int bruteforce(std::vector<int> list); | ||
int hash(std::vector<int> list); | ||
bool mysort(int i, int j); | ||
int sortthencompare(std::vector<int> list); | ||
int binarysearch(std::vector<int> list, int n, int m); | ||
|
||
int bruteforce(std::vector<int> list) | ||
{ | ||
for (auto i = 0; i < list.size(); i++) | ||
{ | ||
for (auto j = i+1; j < list.size(); j++) | ||
{ | ||
if (list[i] == list[j]) | ||
return list[i]; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
int hash(std::vector<int> list) | ||
{ | ||
std::vector<bool> found = {false}; | ||
for (auto i = 0; i < list.size(); i++) | ||
{ | ||
if (found[list[i]]) | ||
return list[i]; | ||
found[list[i]] = true; | ||
} | ||
return -1; | ||
} | ||
|
||
bool mysort(int i, int j) | ||
{ | ||
return i < j; | ||
} | ||
|
||
int sortthencompare(std::vector<int> list) | ||
{ | ||
std::stable_sort(list.begin(), list.end(), mysort); | ||
for (auto i = 0; i < list.size(); i++) | ||
{ | ||
if (list[i] == list[i+1]) | ||
return list[i]; | ||
} | ||
return -1; | ||
} | ||
|
||
int binarysearch(std::vector<int> list, int n, int m) | ||
{ | ||
while (n < m) | ||
{ | ||
int count = 0; | ||
int mid = (n + m) / 2; | ||
for (int item : list) | ||
if (item >= n && item <= mid) | ||
count++; | ||
if (count > mid - n + 1) | ||
m = mid; | ||
else | ||
n = mid + 1; | ||
} | ||
return m; | ||
} | ||
|
||
int main() | ||
{ | ||
std::vector<int> list = {9, 8, 6, 7, 5, 7, 3, 4, 2, 1}; | ||
std::cout << "Expected result: 7" << std::endl; | ||
std::cout << "Brute force: " << bruteforce(list) << std::endl; | ||
std::cout << "Using hash: " << hash(list) << std::endl; | ||
std::cout << "Sort then compare: " << sortthencompare(list) << std::endl; | ||
std::cout << "Binary search: " << binarysearch(list, 1, list.size() - 1) | ||
<< std::endl; | ||
return 0; | ||
} |
File renamed without changes.
File renamed without changes.
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 @@ | ||
factorial :: Integer -> Integer | ||
factorial 0 = 1; | ||
factorial 1 = 1; | ||
factorial x = x * factorial (x - 1) |
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 <stdio.h> | ||
|
||
#define MAX_LEN 100 | ||
|
||
void reverse(char *message); | ||
|
||
int main(void) { | ||
|
||
char message[MAX_LEN]; | ||
char c, *p = message; | ||
|
||
printf("Enter a message: "); | ||
|
||
while ((c = getchar()) != '\n' && p < message + MAX_LEN) | ||
*p++ = c; | ||
*p = '\0'; | ||
|
||
reverse(message); | ||
printf("Reversal is: "); | ||
|
||
printf("%s\n", message); | ||
|
||
return 0; | ||
} | ||
|
||
void reverse(char *message) { | ||
|
||
char *p = message, *q = message, temp; | ||
|
||
while (*q) | ||
q++; | ||
q--; | ||
|
||
while (p < q) { | ||
temp = *p; | ||
*p = *q; | ||
*q = temp; | ||
p++; | ||
q--; | ||
} | ||
} |