Skip to content

Commit

Permalink
added new C programs, moved misplaced files
Browse files Browse the repository at this point in the history
  • Loading branch information
williamgherman committed Oct 11, 2018
1 parent 336e879 commit 2d355ed
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 0 deletions.
32 changes: 32 additions & 0 deletions problems/closed-loops/closed-loops.c
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;
}
79 changes: 79 additions & 0 deletions problems/duplicate-element/duplicate-element.cpp
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.
4 changes: 4 additions & 0 deletions problems/factorial/factorial.hs
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)
41 changes: 41 additions & 0 deletions problems/reverse-string/reverse-string.c
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--;
}
}

0 comments on commit 2d355ed

Please sign in to comment.