├── .gitignore ├── whitespace ├── input.txt └── flightcrank.c ├── polynomial ├── jordan.min.c ├── jordan.c └── flightcrank.c ├── josephus └── jordan.py ├── wordsearch ├── words.txt ├── jordan.py └── flightcrank.c ├── a-block-of-text ├── pt.txt ├── jordan.py ├── angeldm.go └── flightcrank.c ├── binary ├── jordan.py ├── MarkM.py └── flightcrank.c ├── oscillator └── jordan.py ├── vigenere └── jordan.py ├── ig-pay-atin-lay ├── jordan.py └── flightcrank.c ├── an-almost-perfect-circle └── jordan.py ├── table-games ├── jordan.py └── flightcrank.c ├── shoot-me-a-text ├── jordan.py └── flightcrank.c ├── hail-caesar ├── jordan.py ├── MarkM.py └── flightcrank.c ├── spiraling-out-of-control └── jordan.py ├── look-and-say └── jordan.rkt ├── README.md ├── working-with-time ├── jordan.py └── flightcrank.c ├── encrypt └── jordan.c ├── numbers-for-people └── jordan.py ├── wiki-scraper └── jordan.rb ├── digital ├── jordan.py └── flightcrank.c └── a-day-at-the-lanes └── jordan.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | main 3 | *.a 4 | *.pyc 5 | -------------------------------------------------------------------------------- /whitespace/input.txt: -------------------------------------------------------------------------------- 1 | Hello World! 2 | 3 | Goodbye! 4 | 5 | -------------------------------------------------------------------------------- /polynomial/jordan.min.c: -------------------------------------------------------------------------------- 1 | main(int b,char**d){ 2 | int a,n,k=atoi(d[b-1]); 3 | for(n=1;n 1: 6 | a.remove(a[p]) 7 | p += 1 8 | p %= len(a) 9 | 10 | return a[0] 11 | 12 | for i in range(1, 20): 13 | print josephus(i) -------------------------------------------------------------------------------- /wordsearch/words.txt: -------------------------------------------------------------------------------- 1 | hydrogen 2 | helium 3 | lithium 4 | beryllium 5 | boron 6 | carbon 7 | nitrogen 8 | oxygen 9 | fluorine 10 | neon 11 | sodium 12 | magnesium 13 | aluminum 14 | silicon 15 | phosphorous 16 | sulfur 17 | chlorine 18 | argon 19 | potassium 20 | calcium 21 | scandium 22 | titanium 23 | vanadium 24 | chromium 25 | manganese -------------------------------------------------------------------------------- /a-block-of-text/pt.txt: -------------------------------------------------------------------------------- 1 | Program This is a site designed for the everyday code junkie who's caught between great ideas. It is designed to feed off your boredom, and allow you to perfect your craft when you just can't think of something to program. Challenges range from easy to difficult, and will help you understand proper conduct and "almost-real-world" problem solving, while remaining pain-free and overall relaxing. -------------------------------------------------------------------------------- /binary/jordan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | def to_binary(i): 4 | exponent = 0 5 | total = 0 6 | while i > 0: 7 | total += (i % 2) * (10 ** exponent) 8 | i /= 2 9 | exponent += 1 10 | return total 11 | 12 | def main(): 13 | input_string = raw_input() 14 | print ' '.join([str(to_binary(ord(char))).rjust(8, '0') for char in input_string]) 15 | 16 | if __name__ == '__main__': 17 | main() -------------------------------------------------------------------------------- /oscillator/jordan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | from math import sin, pi 4 | from time import sleep 5 | from sys import stdout 6 | 7 | def main(): 8 | t = 0 9 | while 1: 10 | t += 1 11 | width = 60 12 | 13 | display = ['-'] * width 14 | 15 | for speed in range(1, 10): 16 | pos = int((sin(speed * t * pi / 180) + 1) * (width - 1)/2) 17 | display[pos] = 'O' 18 | 19 | stdout.write('\r%s' % ''.join(display)) 20 | stdout.flush() 21 | sleep(0.1) 22 | 23 | if __name__ == '__main__': 24 | main() -------------------------------------------------------------------------------- /binary/MarkM.py: -------------------------------------------------------------------------------- 1 | #Author: Mark McCulloh 2 | #Mark.McCulloh@gmail.com 3 | 4 | def StringToBinary(InputString): 5 | NewString = '' 6 | for Character in InputString: 7 | Number = ord(Character) 8 | Exponent = 7 9 | while Exponent > -1: 10 | if 2 ** Exponent <= Number: 11 | NewString += '1' 12 | Number -= 2 ** Exponent 13 | else: 14 | NewString += '0' 15 | Exponent -= 1 16 | NewString += ' ' 17 | return NewString.rstrip() 18 | 19 | def main(): 20 | print StringToBinary(raw_input()) 21 | 22 | if __name__ == '__main__': 23 | main() 24 | -------------------------------------------------------------------------------- /vigenere/jordan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | from sys import argv, exit 4 | 5 | def main(): 6 | if len(argv) < 3: 7 | print 'Usage: ./vigenere.py ' 8 | exit(1) 9 | 10 | string = argv[1].lower() 11 | key = argv[2].lower() 12 | 13 | s = '' 14 | for i, c in enumerate(string): 15 | if ord(c) < ord('a') or ord(c) > ord('z'): 16 | s += c 17 | continue 18 | index = ord(c) - ord('a') 19 | shift = ord(key[i % len(key)]) - ord('a') 20 | s += chr((index + shift) % 26 + ord('a')) 21 | 22 | print s 23 | 24 | if __name__ == '__main__': 25 | main() -------------------------------------------------------------------------------- /ig-pay-atin-lay/jordan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | def main(): 3 | my_in = raw_input() 4 | my_out = '' 5 | 6 | for word in my_in.split(): 7 | if word[0] in 'aeiou': 8 | front = word 9 | back = 'w' 10 | else: 11 | first = min([word.find(c) for c in 'aeiou' if word.find(c) > -1]) 12 | front = word[:first] 13 | back = word[first:] 14 | if front[0] >= 'A' and front[0] <= 'Z': 15 | front = front.lower() 16 | back = back[0].upper() + back[1:] 17 | 18 | my_out += '%s%say ' % (back, front) 19 | 20 | print my_out 21 | 22 | if __name__ == '__main__': 23 | main() -------------------------------------------------------------------------------- /an-almost-perfect-circle/jordan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | from sys import argv 3 | 4 | def main(): 5 | gradient = '#X&1; ' 6 | ease = 1.4 7 | 8 | if len(argv) > 1: 9 | radius = int(argv[1]) - 1 10 | char = 'X' 11 | 12 | for a in range(radius * 2 + 1): 13 | line = '' 14 | for b in range(radius * 2 + 1): 15 | offset = abs((a-radius)**2 + (b-radius)**2 - radius**2) / float(radius) * len(gradient) 16 | offset /= ease 17 | if offset >= len(gradient): 18 | offset = len(gradient) - 1 19 | 20 | line += gradient[int(offset)] + ' ' 21 | print line 22 | else: 23 | print 'Usage: python circle.py [RADIUS]' 24 | 25 | if __name__ == '__main__': 26 | main() -------------------------------------------------------------------------------- /table-games/jordan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | def main(): 3 | my_input = raw_input("Enter some words, separated by spaces: ") 4 | a = my_input.split() 5 | 6 | title = raw_input("Enter a title: ") 7 | 8 | max_length = len(title) 9 | for elem in a: 10 | if len(elem) > max_length: 11 | max_length = len(elem) 12 | 13 | max_length += 3 14 | output = '+' + '-' * max_length + '+\n' 15 | ws = max_length - len(title) 16 | output += '|' + ' ' * (ws / 2) + title + ' ' * (ws - ws/ 2) + '|\n' 17 | output += '+' + '-' * max_length + '+\n' 18 | 19 | for elem in a: 20 | output += '| ' + elem.ljust(max_length - 1) + '|\n' 21 | 22 | output += '+' + '-' * max_length + '+\n' 23 | 24 | print output 25 | 26 | if __name__ == '__main__': 27 | main() 28 | -------------------------------------------------------------------------------- /shoot-me-a-text/jordan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | def main(): 3 | message = raw_input() 4 | output = '' 5 | 6 | kb = { 2 : 'abc', 3 : 'def', 4 : 'ghi', 5 : 'jkl', 6 : 'mno', 7 : 'pqrs', 8 : 'tyv' , 9 : 'wxyz' } 7 | 8 | for char in message: 9 | if char == ' ': 10 | if len(output) > 0 and output[-1] == '0': 11 | output += ' ' 12 | output += '0' 13 | continue 14 | 15 | for key in kb: 16 | if char <= kb[key][-1]: 17 | i = kb[key].find(char) 18 | if i == -1: 19 | break 20 | else: 21 | if len(output) > 0 and output[-1] == str(key): 22 | output += ' ' 23 | output += str(key) * (i + 1) 24 | break 25 | 26 | print output 27 | 28 | if __name__ == '__main__': 29 | main() -------------------------------------------------------------------------------- /hail-caesar/jordan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | def main(): 3 | string = raw_input('Enter a string: ') 4 | shift = int(raw_input('How much do you want to shift? ')) 5 | 6 | return_string = '' 7 | for char in string: 8 | # get the number of characters from 'a' (adjust for the correct case) 9 | if char >= 'a' and char <= 'z': 10 | base = ord('a') 11 | elif char >= 'A' and char <= 'Z': 12 | base = ord('A') 13 | else: 14 | return_string += char 15 | continue 16 | 17 | code = ord(char) - base 18 | 19 | # add the shift, mod 26 to reduce overflow, then add it back to a 20 | # to get a legitimate ASCII value 21 | new_code = (code + shift) % 26 + base 22 | 23 | # append it to our return string 24 | return_string += chr(new_code) 25 | 26 | print 'Cipher: %s' % return_string 27 | 28 | if __name__ == '__main__': 29 | main() 30 | -------------------------------------------------------------------------------- /a-block-of-text/jordan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | from sys import argv 3 | 4 | def main(): 5 | if len(argv) > 3: 6 | try: 7 | content = open(argv[1]).read() 8 | except IOError: 9 | print 'File not found.' 10 | 11 | width = int(argv[2]) 12 | height = int(argv[3]) 13 | pos = 0 14 | 15 | line = '' 16 | 17 | for char in content: 18 | if char not in '" \'': 19 | line += char 20 | pos += 1 21 | 22 | if pos % width == 0: 23 | if pos % (width * height) == 0: 24 | line += '\n' 25 | print line 26 | line = '' 27 | 28 | print line 29 | else: 30 | print 'Usage: python block_text.py [FILE] [BLOCK WIDTH] [BLOCK HEIGHT]' 31 | 32 | if __name__ == '__main__': 33 | main() -------------------------------------------------------------------------------- /polynomial/flightcrank.c: -------------------------------------------------------------------------------- 1 | //Author: flightcrank 2 | //Email: ocaumaxim@gmail.com 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define MAX 100 10 | 11 | int main() { 12 | 13 | char str[MAX]; 14 | int count,n; 15 | char *in; 16 | int *a; 17 | 18 | printf("Enter number sequence seperated by spaces: "); 19 | fgets(str, MAX - 1, stdin); 20 | 21 | a = (int*) malloc(50); // 50 ints, only need 50 or so because the input string is 100 chars 22 | count = 0; 23 | in = strtok(str," \n"); 24 | 25 | while (in != NULL) { 26 | 27 | sscanf(in,"%d",&n); 28 | a[count] = n; 29 | in = strtok(NULL, " \n"); 30 | count++; 31 | } 32 | 33 | int p = count - 2; //power 34 | int f = 0; // final result 35 | int i; 36 | 37 | for(i = 0; i < count - 1; i++) { 38 | 39 | double r = pow((double) a[count - 1], (double) p); 40 | f += a[i] * r; 41 | p--; 42 | } 43 | 44 | printf("%d\n", f); 45 | 46 | free(a); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /spiraling-out-of-control/jordan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | def main(): 3 | length = int(raw_input('Enter an odd side length: ')) 4 | while length % 2 == 0: 5 | length = int(raw_input('Enter an odd side length: ')) 6 | 7 | spiral = []*length 8 | for a in range(length): 9 | spiral.append([-1]*length) 10 | 11 | r, c = length / 2, length / 2 12 | dr, dc = -1, 0 13 | v = 0 14 | for i in range(length**2): 15 | spiral[r][c] = chr(v + ord('a')) 16 | 17 | if dr == 0: 18 | look_r = dc 19 | look_c = 0 20 | else: 21 | look_r = 0 22 | look_c = -dr 23 | 24 | if spiral[r+look_r][c+look_c] == -1: 25 | dr = look_r 26 | dc = look_c 27 | 28 | r += dr 29 | c += dc 30 | 31 | v += 1 32 | v %= 26 33 | 34 | for line in spiral: 35 | print ' '.join(map(str, line)) 36 | 37 | if __name__ == '__main__': 38 | main() -------------------------------------------------------------------------------- /look-and-say/jordan.rkt: -------------------------------------------------------------------------------- 1 | #lang racket 2 | (define (look-and-say n) 3 | (look-and-say-helper n 1)) 4 | 5 | (define (look-and-say-helper n a) 6 | (if (= n 0) 7 | a 8 | (look-and-say-helper (- n 1) (int-from-digits (compute-count (split a)))))) 9 | 10 | (define (int-from-digits ls) 11 | (int-from-digits-helper 0 (reverse ls))) 12 | 13 | (define (int-from-digits-helper ex ls) 14 | (if (null? ls) 15 | 0 16 | (+ (* (car ls) (expt 10 ex)) (int-from-digits-helper (+ ex 1) (cdr ls))))) 17 | 18 | (define (split a) 19 | (reverse (split-helper a))) 20 | 21 | (define (split-helper a) 22 | (if (= a 0) 23 | '() 24 | (cons (modulo a 10) (split-helper (floor (/ a 10)))))) 25 | 26 | (define (compute-count ls) 27 | (compute-count-helper (cdr ls) (car ls) 1)) 28 | 29 | (define (compute-count-helper list element run) 30 | (if (null? list) 31 | (cons run (cons element '())) 32 | (if (= element (car list)) 33 | (compute-count-helper (cdr list) element (+ run 1)) 34 | (cons run (cons element (compute-count-helper (cdr list) (car list) 1)))))) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Program This 2 | 3 | This repository hosts all of my solutions to the challenges posted to [programthis](http://programthis.net). Be sure to check back often for updates. 4 | 5 | ## To add your own solutions 6 | 7 | Sharing your solutions with the world is actually pretty easy, just follow these steps. Confused about some of them? Speak up, and contact me. 8 | 9 | * Fork this repo 10 | * Go into the folder of the challenge you have solved 11 | * Place your code in the folder 12 | * Rename your code as `username`.`extension` (if your username is JohnD and you wrote this in Python - `johnd.py`) 13 | * Commit and push updates to your fork 14 | * Submit a pull request with a title like `JohnD's solution to a-day-at-the-lanes` 15 | 16 | And that's all! Be sure to include comments in your code including your contact info, the date, and anything else you'd like to add. If I *really* like it, you 17 | might just get a little something. 18 | 19 | ## Contact 20 | 21 | Feel free to make use of the chat at the bottom of the website. I'm usually around! 22 | 23 | Questions? Comments? Chit chat? Feel free to message me anytime at `jordan(at)programthis.net` 24 | 25 | Submission? `submit(at)programthis.net` -------------------------------------------------------------------------------- /a-block-of-text/angeldm.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "bytes" 6 | "flag" 7 | "fmt" 8 | "io" 9 | "os" 10 | ) 11 | 12 | var fileName string 13 | var height int 14 | var width int 15 | 16 | func main() { 17 | flag.StringVar(&fileName, "text", "text", "Use a valid filename") 18 | flag.IntVar(&height, "height", 5, "Use a valid height") 19 | flag.IntVar(&width, "width", 20, "Use a valid width") 20 | flag.Parse() 21 | if fileName == "" { 22 | panic("File not found") 23 | } 24 | if width < 1 { 25 | panic("Invalid width") 26 | } 27 | if height < 1 { 28 | panic("Invalid height") 29 | } 30 | 31 | file, err := os.Open(fileName) 32 | if err != nil { 33 | panic(err) 34 | } 35 | 36 | reader := bufio.NewReader(file) 37 | buffer := bytes.NewBuffer(make([]byte, 1024)) 38 | pos := 0 39 | for { 40 | c, err := reader.ReadByte() 41 | if err == io.EOF { 42 | break 43 | } 44 | if c != '\'' && c != ' ' && c != '"' { 45 | buffer.WriteByte(c) 46 | pos++ 47 | if pos%width == 0 { 48 | buffer.WriteByte('\n') 49 | } 50 | if pos % (width * height) == 0 { 51 | buffer.WriteByte('\n') 52 | } 53 | } 54 | 55 | } 56 | fmt.Println(buffer) 57 | 58 | } 59 | -------------------------------------------------------------------------------- /a-block-of-text/flightcrank.c: -------------------------------------------------------------------------------- 1 | //Author: flightcrank 2 | //Email: ocaumaxim@gmail.com 3 | 4 | //note user must edit source file to chage line width/height 5 | 6 | #include 7 | 8 | #define WIDTH 20 9 | #define HEIGHT 5 10 | 11 | int main() { 12 | 13 | char current; 14 | 15 | //int width = 20; 16 | //int height = 5; 17 | int w_count = 0; 18 | int h_count = 0; 19 | 20 | //open file to process 21 | FILE *fp; 22 | fp = fopen("pt.txt","r"); 23 | 24 | if (fp == NULL) { 25 | 26 | printf("error opening file. does pt.txt exist ?"); 27 | return 1; // exit porgram 28 | } 29 | 30 | //loop throught each char in the file. 31 | do { 32 | current = fgetc(fp); 33 | 34 | if (current != ' ' && current != '"' && current != '\'') { 35 | 36 | printf("%c", current); 37 | w_count++; 38 | 39 | //max line width reached, reset 40 | if (w_count == WIDTH) { 41 | 42 | printf("\n"); 43 | w_count = 0; 44 | h_count++; 45 | } 46 | 47 | //max block height reached, reset 48 | if(h_count == HEIGHT) { 49 | 50 | printf("\n\n"); 51 | h_count = 0; 52 | } 53 | } 54 | 55 | } while(current != EOF); 56 | 57 | //close file 58 | fclose(fp); 59 | 60 | return 0; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /working-with-time/jordan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | def main(): 4 | months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] 5 | days_of_wk = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] # start on Monday 6 | days_in_mo = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 7 | 8 | # January 1st, 1 AD was a Monday 9 | month = int(raw_input("Enter a month (1-12): ")) 10 | day = int(raw_input("Enter a day (1-%s): " % days_in_mo[month-1])) 11 | year = int(raw_input("Enter a year (> 0): ")) 12 | 13 | days_since = 365 * (year - 1) # 365 days for each year up until the current year (we started at year 1) 14 | for m in range(0, (month - 1)): 15 | days_since += days_in_mo[m] # add all the days up until the current month 16 | 17 | days_since += day # add in the days from the current month 18 | 19 | # don't forget about leap years! 20 | # 1 day for every 4 years except centuries EXCEPT years divisible by 400 21 | 22 | days_since += (year / 4) - (year / 100) + (year / 400) 23 | days_since -= 1 # since we started on January 1st, 1 AD 24 | 25 | print '%s %s, %s is a %s' % (months[month-1], day, year, days_of_wk[days_since % 7]) 26 | 27 | if __name__ == '__main__': 28 | main() -------------------------------------------------------------------------------- /hail-caesar/MarkM.py: -------------------------------------------------------------------------------- 1 | #Author: Mark McCulloh 2 | #Mark.McCulloh@gmail.com 3 | 4 | from os.path import * 5 | 6 | def CC_ShiftLetter(Character, Shift): 7 | Start = 0 8 | if Character >= 'a' and Character <= 'z': Start = 97 9 | elif Character >= 'A' and Character <= 'Z': Start = 65 10 | else: return Character 11 | return chr(((ord(Character) - Start) + Shift) % 26 + Start) 12 | 13 | #If (File=True), InputString must be a filename (test1.txt) 14 | #Encrypted string is stored in 'CC_' + filename (CC_test1.txt) 15 | def CaesarCipher(InputString, Shift = 13, File = False): 16 | Name = InputString 17 | if File and isfile(Name): 18 | InputFile = open(Name) 19 | InputString = InputFile.read() 20 | InputFile.close() 21 | NewString = '' 22 | for Character in InputString: 23 | NewString += CC_ShiftLetter(Character, Shift) 24 | if File and isfile(Name): 25 | NewFile = open('CC_' + Name, 'w') 26 | NewFile.write(NewString) 27 | NewFile.close() 28 | return NewString 29 | 30 | #'Dumb' Decryption, returns a list of all possible strings 31 | def CC_Decrypt(InputString): 32 | Possibles = [] 33 | for i in range(26): 34 | Possibles.append(CaesarCipher(InputString, i)) 35 | return Possibles 36 | 37 | def main(): 38 | UserString = raw_input('What is the string you want to encrypt? ') 39 | Shifts = int(raw_input('How many shifts? ')) 40 | print CaesarCipher(UserString, Shifts) 41 | raw_input() 42 | 43 | if __name__ == '__main__': 44 | main() 45 | -------------------------------------------------------------------------------- /encrypt/jordan.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char **argv) { 6 | char buff[1024]; 7 | char filename[256]; 8 | char command[1024]; 9 | 10 | char *key; 11 | int n, i, key_len; 12 | int fd, fd2; 13 | 14 | if (argc < 2) { 15 | printf("Please list a file to encrypt.\n"); 16 | exit(0); 17 | } 18 | 19 | strncpy(filename, argv[1], 256 - strlen(".encrypt")); 20 | strcat(filename, ".encrypt"); 21 | 22 | if (argc < 3) { 23 | write(1, "Enter an encryption key: ", 25); 24 | while((n = read(0, buff, 257)) == 0) 25 | printf("Try again.\n"); 26 | 27 | buff[n-1] = '\0'; 28 | key = strdup(buff); 29 | } else 30 | key = strdup(argv[2]); 31 | 32 | key_len = strlen(key); 33 | 34 | if ((fd = open(argv[1], O_RDONLY)) < 0) 35 | printf("Error: file not found.\n"); 36 | 37 | fd2 = open(filename, O_WRONLY | O_CREAT); 38 | 39 | while ((n = read(fd, buff, 1024)) > 0) { 40 | for (i = 0; i < n; i++) 41 | buff[i] ^= key[i % key_len]; 42 | 43 | write(fd2, buff, n); 44 | } 45 | 46 | close(fd); 47 | close(fd2); 48 | 49 | // chmod the file 50 | chmod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 51 | 52 | // now move the encypted file to the original 53 | strcpy(command, "mv "); 54 | strcat(command, filename); 55 | strcat(command, " "); 56 | strcat(command, argv[1]); 57 | 58 | system(command); 59 | } -------------------------------------------------------------------------------- /ig-pay-atin-lay/flightcrank.c: -------------------------------------------------------------------------------- 1 | //Author: flightcrank 2 | //Email: ocaumaxim@gmail.com 3 | 4 | #include 5 | #include 6 | 7 | #define MAX_INPUT 101 8 | 9 | //find the first occurance of a vowel in a string and retun its index 10 | int first_vowel(char *str) { 11 | 12 | int i; 13 | 14 | for(i = 0; i < strlen(str); i++) { 15 | 16 | switch (str[i]) { 17 | 18 | case 'a': 19 | 20 | return i; 21 | case 'e': 22 | 23 | return i; 24 | case 'i': 25 | 26 | return i; 27 | case 'o': 28 | 29 | return i; 30 | case 'u': 31 | 32 | return i; 33 | } 34 | } 35 | 36 | return 0; 37 | } 38 | 39 | int main() { 40 | 41 | char str[MAX_INPUT]; 42 | char *p; 43 | 44 | puts("enter string"); 45 | fgets(str, MAX_INPUT, stdin); 46 | 47 | //sreach the input and break it up into words 48 | p = strtok(str, " \n"); 49 | 50 | //loop through all the words found 51 | while(p != NULL) { 52 | 53 | int len = strlen(p); 54 | int index = 0; 55 | int i,j; 56 | 57 | index = first_vowel(p); 58 | 59 | //words that dont start with a vowel 60 | if (index > 0) { 61 | 62 | //print chars from first vowel onwards 63 | for (i = index; i < len; i++) { 64 | 65 | printf("%c", p[i]); 66 | 67 | } 68 | 69 | //add the first char to the first vowel 70 | for (j = 0; j < index; j++) { 71 | 72 | printf("%c", p[j]); 73 | 74 | } 75 | 76 | printf("ay\n"); 77 | 78 | //the word must start with a vowel 79 | } else { 80 | 81 | printf("%sway\n",p); 82 | } 83 | 84 | //search next word in the input 85 | p = strtok(NULL, " \n"); 86 | } 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /whitespace/flightcrank.c: -------------------------------------------------------------------------------- 1 | //Author: flightcrank 2 | //Email: ocaumaxim@gmail.com 3 | 4 | 5 | //NOTE: just prints to stdout, use redirection operator '>' to output to a new file 6 | 7 | #include 8 | #include 9 | 10 | #define MAX 101 11 | 12 | int main () { 13 | 14 | //line buffer 15 | char line[MAX]; 16 | 17 | //open file to process 18 | FILE *fp; 19 | fp = fopen("input.txt","r"); 20 | 21 | if (fp == NULL) { 22 | 23 | printf("error opening file. does pt.txt exist ?"); 24 | return 1; // exit porgram 25 | } 26 | 27 | //loop throught each line in the file. 28 | while (fgets(line, MAX - 1, fp) != NULL) { 29 | 30 | int i; 31 | 32 | //search string in reverse (skipping the null byte strlen includes in the length) 33 | //when were @ index 0 we dont want to check index -1, out of bounds 34 | //for condition will not be met at case i = 0 35 | for (i = strlen(line) - 1; i > 0; i--) { 36 | 37 | //fgets seems to add a '\n' if there isnt one. 38 | //so the last line will contain a '\n' aswell 39 | if (line[i] == '\n') { 40 | 41 | //if whitespace is found, replace with the newline 42 | //and end string with a new null byte 43 | if (line[i - 1] == ' ' || line[i - 1] == '\t') { 44 | 45 | line[i - 1] = '\n'; 46 | line[i] = 0; 47 | 48 | } else { 49 | //whitespace not found on the char to the left 50 | //so none to remove, breakout of loop early 51 | //to avoid looping strnlen() number of times 52 | break; 53 | } 54 | } 55 | } 56 | 57 | //print out line (or write/append to a file) 58 | printf("%s", line); 59 | } 60 | 61 | //close file 62 | fclose(fp); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /shoot-me-a-text/flightcrank.c: -------------------------------------------------------------------------------- 1 | //Author: flightcrank 2 | //Email: ocaumaxim@gmail.com 3 | 4 | #include 5 | #include 6 | 7 | //find out how many times we need to press a key to get the correct char 8 | //return a 2 value array with index 0 being the key to press and index 1 9 | //being the amount of times to press it 10 | int* char_to_keypress(char c) { 11 | 12 | //array of strings with the array index number, acting at the key pad number 13 | char *key_pad[] = {" ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 14 | //has to be static to it lasts for the duration of the program, not just duration of the function 15 | static int key_amount[2]; 16 | 17 | key_amount[0] = 0; 18 | key_amount[1] = 0; 19 | int i,j; 20 | 21 | //loops through the keypad numbers 22 | for(i = 0; i < 10; i++) { 23 | 24 | //loop through the strings of the current keypad number 25 | for(j = 0; j < strlen(key_pad[i]); j++) { 26 | 27 | //if we find the char were looking for 28 | if (key_pad[i][j] == c) { 29 | 30 | key_amount[0] = i; //keypad number 31 | key_amount[1] = j + 1;//number of times we press key to get the char 32 | return key_amount; 33 | } 34 | } 35 | } 36 | //char not on keypad 37 | return NULL; 38 | } 39 | 40 | int main() { 41 | 42 | char str[101]; 43 | puts("enter string:"); 44 | fgets(str,101,stdin); 45 | 46 | int i,j; 47 | int last = 0; 48 | int *val; 49 | 50 | for (i = 0; i < strlen(str); i++) { 51 | 52 | val = char_to_keypress(str[i]); 53 | 54 | if (val != NULL) { 55 | 56 | if (val[0] == last) { 57 | 58 | printf(" "); 59 | } 60 | 61 | for (j = 0; j < val[1]; j++) { 62 | 63 | printf("%d",val[0]); 64 | } 65 | 66 | last = val[0]; 67 | } 68 | } 69 | 70 | printf("\n"); 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /numbers-for-people/jordan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | from sys import argv 3 | 4 | def number_to_human(n): 5 | biggest = ['', '', 'thousand', 'million', 'billion', 'trillion', 6 | 'quadrillion', 'quintillion', 'sextillion', 7 | 'septillion', 'octillion', 'nonillion', 'decillion'] 8 | 9 | tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 10 | 'seventy', 'eighty', 'ninety'] 11 | 12 | teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 13 | 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] 14 | 15 | digits = ['', 'one', 'two', 'three', 'four', 'five', 16 | 'six', 'seven', 'eight', 'nine'] 17 | 18 | out = '' 19 | 20 | # isolate the digits of n into groups of three 21 | digit_groups = [] 22 | while n > 0: 23 | digit_groups.append(n % 1000) 24 | n /= 1000 25 | digit_groups.reverse() 26 | 27 | for i, k in enumerate(digit_groups): 28 | if k != 0: 29 | if k > 99: 30 | out += digits[k / 100] + ' hundred ' 31 | k %= 100 32 | 33 | if k > 19: 34 | out += tens[k / 10] 35 | k %= 10 36 | 37 | if k > 0: 38 | out += '-' + digits[k] 39 | elif k > 9: 40 | out += teens[k - 10] 41 | else: 42 | out += digits[k] 43 | 44 | if i != len(digit_groups) - 1: 45 | out += ' ' + biggest[len(digit_groups) - i] + ' ' 46 | 47 | return out 48 | 49 | if __name__ == '__main__': 50 | if len(argv) < 2: 51 | print 'Please include a number to translate' 52 | else: 53 | print number_to_human(int(argv[1])) 54 | 55 | -------------------------------------------------------------------------------- /wiki-scraper/jordan.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | # wiki-scraper.rb by Jordan Scales 4 | # http://jordanscales.com 5 | # http://programthis.net 6 | # 7 | # Tests the idea that the first link on each wikipedia article 8 | # will eventually lead to philosophy 9 | # 10 | # Usage: 11 | # ruby wiki-scraper.rb daft punk 12 | 13 | require 'nokogiri' 14 | require 'open-uri' 15 | require 'cgi' 16 | 17 | ROOT_URL = 'http://en.wikipedia.org' 18 | 19 | def search_url(query) 20 | "http://en.wikipedia.org/w/index.php?search=#{CGI.escape(query)}" 21 | end 22 | 23 | def title_from_url(url) 24 | doc = Nokogiri::HTML(open(url)) 25 | doc.css('h1#firstHeading').first.content 26 | end 27 | 28 | def title_from_query(query) 29 | title_from_url search_url(query) 30 | end 31 | 32 | def first_link(url) 33 | doc = Nokogiri::HTML(open(url)) 34 | parenth = 0 35 | 36 | # cycle through each paragraph 37 | doc.css('div.mw-content-ltr > p').each do |p| 38 | 39 | # in each paragraph, go through each node 40 | p.children.each do |c| 41 | 42 | # if we've found two parentheses, return the next link you see 43 | if parenth == 0 or (parenth > 1 and (parenth % 2 == 0)) 44 | if c.name == 'a' 45 | return ROOT_URL + c.attributes["href"].value 46 | end 47 | end 48 | 49 | # incremement the number of parentheses we've seen 50 | if /\(/ === c.to_s 51 | parenth += 1 52 | elsif /\)/ === c.to_s 53 | parenth += 1 54 | end 55 | end 56 | end 57 | end 58 | 59 | def first_link_from_query(query) 60 | first_link search_url(query) 61 | end 62 | 63 | start = ARGV.join(' ') 64 | url = search_url start 65 | title = title_from_url url 66 | puts "1: #{title}" 67 | count = 2 68 | 69 | while title != 'Philosophy' 70 | url = first_link url 71 | title = title_from_url url 72 | puts "#{count}: #{title}: #{url}" 73 | count += 1 74 | end 75 | -------------------------------------------------------------------------------- /hail-caesar/flightcrank.c: -------------------------------------------------------------------------------- 1 | //Author: flightcrank 2 | //Email: ocaumaxim@gmail.com 3 | 4 | #include 5 | 6 | #define MAX 51 7 | 8 | int main() { 9 | 10 | char str[MAX]; 11 | char cypher[MAX]; 12 | int shift = 0; 13 | 14 | puts("enter string: "); 15 | fgets(str, MAX, stdin); 16 | 17 | puts("shift amount: "); 18 | fscanf(stdin, "%d", &shift); 19 | 20 | int i; 21 | 22 | //loop through the sting 23 | for (i = 0; i < MAX; i++) { 24 | 25 | //peek 1 char ahead, null byte means were at the end of the string 26 | if(str[i + 1] == 0) { 27 | 28 | //fgets includes the newline, replace the newline with a null byte 29 | cypher[i] = 0; 30 | break; 31 | } 32 | 33 | //calculate the new caesar acsii value based upon the shift amount 34 | //and wrap around at ascii value 122 which is 'z' 35 | int ans = 0; 36 | 37 | //lowercase ascii value 38 | if (str[i] >= 97 && str[i] <= 122) { 39 | 40 | //ascii 'a' starts at value 97 so if the current ascii char plus the shift amount is 41 | //under 122 all we have to do is add the shift amount 42 | if (str[i] + shift < 122) { 43 | 44 | ans = str[i] + shift; 45 | cypher[i] = ans; 46 | 47 | //if the shift amount puts us past the char 'z' we have to wrap 48 | //around back to 'a' so we calculate how many positions we have to 49 | //move with modulus arithmatic. 50 | //then we add 96 because thats the ascii value where 'a' starts. 51 | } else { 52 | 53 | ans = (str[i] + shift) % 122; 54 | ans += 96; 55 | cypher[i] = ans; 56 | } 57 | 58 | //if the ascii value is uppercase 59 | } else if (str[i] >= 65 && str[i] <= 90) { 60 | 61 | if (str[i] + shift < 90) { 62 | 63 | ans = str[i] + shift; 64 | cypher[i] = ans; 65 | 66 | } else { 67 | 68 | ans = (str[i] + shift) % 90; 69 | ans += 64; 70 | cypher[i] = ans; 71 | } 72 | //ignore all other ascii chars and leave them as is 73 | } else { 74 | 75 | cypher[i] = str[i]; 76 | } 77 | 78 | }//for loop 79 | 80 | puts(cypher); 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /binary/flightcrank.c: -------------------------------------------------------------------------------- 1 | //Author: flightcrank 2 | //Email: ocaumaxim@gmail.com 3 | 4 | //convert ascii values into there binary representation 5 | 6 | #include 7 | 8 | #define MAX 51 9 | 10 | void print_binary_byte(char num) { 11 | 12 | //no ascii number is bigger than 127 13 | //so we know that if we have to print out 14 | //8 binary digets the first one will always be zero. 15 | 16 | printf("0"); 17 | 18 | if (num >= 64) { 19 | 20 | printf("1"); 21 | num = num - 64; 22 | 23 | } else { 24 | 25 | printf("0"); 26 | } 27 | 28 | if (num >= 32 && num < 64) { 29 | 30 | printf("1"); 31 | num = num - 32; 32 | 33 | } else { 34 | 35 | printf("0"); 36 | 37 | } 38 | 39 | if (num >= 16 && num < 32) { 40 | 41 | printf("1"); 42 | num = num - 16; 43 | 44 | } else { 45 | 46 | printf("0"); 47 | } 48 | 49 | if (num >= 8 && num < 16) { 50 | 51 | printf("1"); 52 | num = num - 8; 53 | 54 | } else { 55 | 56 | printf("0"); 57 | } 58 | 59 | if (num >= 4 && num < 8) { 60 | 61 | printf("1"); 62 | num = num - 4; 63 | 64 | } else { 65 | 66 | printf("0"); 67 | } 68 | 69 | if (num >= 2 && num < 4) { 70 | 71 | printf("1"); 72 | num = num - 2; 73 | 74 | } else { 75 | 76 | printf("0"); 77 | } 78 | 79 | if (num == 1) { 80 | 81 | printf("1"); 82 | 83 | } else { 84 | 85 | printf("0"); 86 | } 87 | } 88 | 89 | int main() { 90 | 91 | //string length = MAX 92 | char str[MAX]; 93 | 94 | puts("enter any ascii chars"); 95 | 96 | fgets(str, MAX, stdin); 97 | 98 | int i; 99 | 100 | for (i = 0; i < MAX; i++) { 101 | 102 | //peek at the next char value in the string 103 | //if its a null byte were at the end of the string 104 | //so exit loop early. fgets keeps the newline char. 105 | //so the current value must be newline, skip that too. 106 | if (str[i + 1] == 0) { 107 | break; 108 | } 109 | 110 | print_binary_byte(str[i]); 111 | printf(" "); 112 | } 113 | 114 | printf("\n"); 115 | 116 | return 0; 117 | } 118 | -------------------------------------------------------------------------------- /wordsearch/jordan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import sys 3 | import random 4 | 5 | def main(): 6 | if len(sys.argv) < 2: 7 | print 'Usage: ./wordfind.py word1 [word2] [word3] ...' 8 | print 'ie: ./wordfind.py `cat words.txt`' 9 | sys.exit(1) 10 | 11 | if sys.argv[1] == 'debug': 12 | words = sys.argv[2:] 13 | else: 14 | words = sys.argv[1:] 15 | 16 | r = c = 22 17 | 18 | # create the grid 19 | grid = [] 20 | for a in range(r): 21 | row = [] 22 | for b in range(c): 23 | row.append(' ') 24 | grid.append(row) 25 | 26 | # place the words 27 | for word in words: 28 | while 1: 29 | # choose random orientation 30 | if random.random() > 0.5: # horizontal 31 | row = random.randint(0, r-1) 32 | col = random.randint(0, c - len(word) - 1) 33 | 34 | fits = True 35 | for i, ch in enumerate(word): 36 | if (grid[row][col + i] == ' ') or (grid[row][col + i] == ch): 37 | pass 38 | else: 39 | fits = False 40 | break 41 | 42 | if fits: 43 | for i, ch in enumerate(word): 44 | grid[row][col + i] = ch 45 | break 46 | 47 | else: # vertical 48 | row = random.randint(0, r - len(word) - 1) 49 | col = random.randint(0, c - 1) 50 | 51 | fits = True 52 | for i, ch in enumerate(word): 53 | if (grid[row + i][col] == ' ') or (grid[row + i][col] == ch): 54 | pass 55 | else: 56 | fits = False 57 | break 58 | 59 | if fits: 60 | for i, ch in enumerate(word): 61 | grid[row + i][col] = ch 62 | break 63 | 64 | if sys.argv[1] != 'debug': 65 | letters = 'abcdefghijklmnopqrstuvwxyz' 66 | for a in range(r): 67 | for b in range(c): 68 | if grid[a][b] == ' ': 69 | grid[a][b] = letters[random.randint(0, 25)] 70 | 71 | for row in grid: 72 | print ' '.join(row) 73 | 74 | print ' ' 75 | print '\n'.join(words) 76 | 77 | if __name__ == '__main__': 78 | main() -------------------------------------------------------------------------------- /digital/jordan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # digital.py 3 | # Jordan Scales 4 | # http://programthis.net 5 | 6 | # Synopsis: display numbers and letters on a 7-panel LED (like an alarm clock) 7 | # ____ 8 | # _|__0_|_ 9 | # | | | | 10 | # |1|____|2| 11 | # _|__3_|_ 12 | # | | | | 13 | # |4|____|5| 14 | # |__6_| 15 | # 16 | # 6 x 7 grid 17 | 18 | 19 | # Example: The number 2 20 | 21 | # XXXX 22 | # X 23 | # X 24 | # XXXX 25 | # X 26 | # X 27 | # XXXX 28 | # 29 | # cells 0, 2, 3, 4, and 6 filled in 30 | 31 | numbers = { '0' : '012456', 32 | '1' : '25', 33 | '2' : '02346', 34 | '3' : '02356', 35 | '4' : '1325', 36 | '5' : '01356', 37 | '6' : '013456', 38 | '7' : '025', 39 | '8' : '0123456', 40 | '9' : '01235' } 41 | 42 | def main(): 43 | my_input = raw_input() 44 | panels = [] 45 | 46 | for char in my_input: 47 | if char in numbers: 48 | panels.append(numbers[char]) 49 | 50 | # generate a 2D array for output 51 | output = [] 52 | for row in range(7): 53 | output.append([' '] * len(panels) * 7) 54 | 55 | for i in range(len(panels)): 56 | for cell in panels[i]: 57 | if cell == '0': 58 | output[0][7 * i + 1] = 'X' 59 | output[0][7 * i + 2] = 'X' 60 | output[0][7 * i + 3] = 'X' 61 | output[0][7 * i + 4] = 'X' 62 | elif cell == '1': 63 | output[1][7 * i] = 'X' 64 | output[2][7 * i] = 'X' 65 | elif cell == '2': 66 | output[1][7 * i + 5] = 'X' 67 | output[2][7 * i + 5] = 'X' 68 | elif cell == '3': 69 | output[3][7 * i + 1] = 'X' 70 | output[3][7 * i + 2] = 'X' 71 | output[3][7 * i + 3] = 'X' 72 | output[3][7 * i + 4] = 'X' 73 | elif cell == '4': 74 | output[4][7 * i] = 'X' 75 | output[5][7 * i] = 'X' 76 | elif cell == '5': 77 | output[4][7 * i + 5] = 'X' 78 | output[5][7 * i + 5] = 'X' 79 | elif cell == '6': 80 | output[6][7 * i + 1] = 'X' 81 | output[6][7 * i + 2] = 'X' 82 | output[6][7 * i + 3] = 'X' 83 | output[6][7 * i + 4] = 'X' 84 | 85 | print 86 | for row in output: 87 | print ''.join(row) 88 | print 89 | 90 | if __name__ == '__main__': 91 | main() 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /a-day-at-the-lanes/jordan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | from sys import argv 3 | 4 | def main(): 5 | score = 0 6 | if len(argv) < 2: 7 | print 'Usage: python bowling.py MARKS' 8 | print '(i.e. python bowling.py XXXXXXXXXXXX)' 9 | return 10 | 11 | c = argv[1] 12 | 13 | for i in range(len(c)): 14 | # see if we're in the tenth frame 15 | tenth_frame = False 16 | if c[-3] == 'X' or c[-2] == '/': 17 | # then we have 3 shots in the 10th frame 18 | if i >= len(c) - 3: 19 | tenth_frame = True 20 | else: 21 | # we have 2 shots in the 10th frame 22 | if i >= len(c) - 2: 23 | tenth_frame = True 24 | 25 | if tenth_frame: 26 | # we're in the tenth frame 27 | if c[i] == 'X': 28 | score += 10 29 | elif c[i] == '/': 30 | prev = c[i-1] 31 | if prev == '-': 32 | prev = 0 33 | else: 34 | prev = int(prev) 35 | 36 | score += 10 - prev 37 | elif c[i] == '-': 38 | pass 39 | else: 40 | score += int(c[i]) 41 | else: 42 | # we're not 43 | if c[i] == 'X': 44 | # strike! 45 | score += 10 46 | # calculate the next two shots, and add those as well 47 | shot1 = c[i+1] 48 | shot2 = c[i+2] 49 | if shot1 == 'X': 50 | shot1 = 10 51 | elif shot1 == '-': 52 | shot1 = 0 53 | else: 54 | shot1 = int(shot1) 55 | 56 | if shot2 == 'X': 57 | shot2 = 10 58 | elif shot2 == '/': 59 | shot2 = 10 - shot1 60 | elif shot2 == '-': 61 | shot2 = 0 62 | else: 63 | shot2 = int(shot2) 64 | 65 | score += shot1 + shot2 66 | 67 | elif c[i] == '/': 68 | # spare! 69 | # if it's a spare, the previous character HAS to be a number (or a dash) 70 | prev = c[i-1] 71 | if prev == '-': 72 | prev = 0 73 | else: 74 | prev = int(prev) 75 | 76 | score += 10 - prev 77 | 78 | # find the next shot, and add that as well 79 | shot1 = c[i+1] 80 | # it can be a strike, dash, or number 81 | if shot1 == 'X': 82 | shot1 = 10 83 | elif shot1 == '-': 84 | shot1 = 0 85 | else: 86 | shot1 = int(shot1) 87 | 88 | score += shot1 89 | 90 | elif c[i] == '-': 91 | # gutter ball :( 92 | pass 93 | 94 | else: 95 | # just a number 96 | score += int(c[i]) 97 | 98 | print score 99 | 100 | if __name__ == '__main__': 101 | main() -------------------------------------------------------------------------------- /digital/flightcrank.c: -------------------------------------------------------------------------------- 1 | //Author: flightcrank 2 | //Email: ocaumaxim@gmail.com 3 | 4 | #include 5 | #include 6 | 7 | 8 | //number of rows 9 | #define NUM_ROW 7 10 | 11 | //number of colums 12 | //each number is 7 chars wide, * 10 means a MAX of 10 chars can be printed, 13 | // and + another 10 for a 1 index thick space between printed chars 14 | #define NUM_COL 7 * 10 + 10 15 | 16 | char grid[NUM_ROW][NUM_COL]; 17 | 18 | //prints a single 7 segment LCD char to a 7 * 7 array segemnt. 19 | void display(char *segment, int col_index) { 20 | 21 | int i; 22 | int j = col_index; 23 | int col_max = col_index + 7; 24 | 25 | for (i = 0; i < NUM_ROW; i++) { 26 | 27 | for(j = col_index; j < col_max; j++) { 28 | 29 | //top segement 30 | if (segment[0] != '0') { 31 | 32 | if (i == 0) { 33 | 34 | grid[i][j] = 'X'; 35 | continue; 36 | } 37 | } 38 | 39 | //top right segment 40 | if (segment[1] != '0') { 41 | 42 | if (i > 0 && i < 3) { 43 | 44 | if (j == col_index + 6) { 45 | 46 | grid[i][j] = 'X'; 47 | continue; 48 | } 49 | } 50 | } 51 | 52 | //top left segment 53 | if (segment[2] != '0') { 54 | 55 | if (i > 0 && i < 3) { 56 | 57 | if (j == col_index) { 58 | 59 | grid[i][j] = 'X'; 60 | continue; 61 | } 62 | } 63 | } 64 | 65 | //middle segment 66 | if (segment[3] != '0') { 67 | 68 | if (i == 3) { 69 | 70 | grid[i][j] = 'X'; 71 | continue; 72 | } 73 | } 74 | 75 | //bottom right segment 76 | if (segment[4] != '0') { 77 | 78 | if (i > 3 && i < 6) { 79 | 80 | if (j == col_index + 6) { 81 | 82 | grid[i][j] = 'X'; 83 | continue; 84 | } 85 | } 86 | } 87 | 88 | //bottomleft segment 89 | if (segment[5] != '0') { 90 | 91 | if (i > 3 && i < 6) { 92 | 93 | if (j == col_index) { 94 | 95 | grid[i][j] = 'X'; 96 | continue; 97 | } 98 | } 99 | } 100 | 101 | //bottom segment 102 | if (segment[6] != '0') { 103 | 104 | if (i == NUM_ROW - 1) { 105 | 106 | grid[i][j] = 'X'; 107 | continue; 108 | } 109 | } 110 | 111 | //prints a space in the void areas of the 7*7 grid 112 | grid[i][j] = ' '; 113 | } 114 | } 115 | } 116 | 117 | //simply prints each char in the array to stdout 118 | void print_lcd() { 119 | 120 | int i,j; 121 | 122 | for (i = 0; i < NUM_ROW; i++) { 123 | 124 | for(j = 0; j < NUM_COL; j++) { 125 | 126 | 127 | if (grid[i][j] == 0) { 128 | 129 | printf(" "); 130 | 131 | } else { 132 | 133 | printf("%c", grid[i][j]); 134 | } 135 | } 136 | 137 | printf("\n"); 138 | } 139 | } 140 | 141 | int main() { 142 | 143 | //each index represents number 0 through to 9 144 | //each 1 represents a segment to draw 145 | //each 0 represents a segment not to draw 146 | char *numbers[] = {"1110111", "0100100","1101011","1101101","0111100", 147 | "1011101", "1011111","1100100","1111111","1111100"}; 148 | 149 | int i; 150 | int col = 0; 151 | char input[12]; 152 | 153 | //no error checking ONLY input numbers or ascii char > 48 otherwise it will segfault 154 | puts("enter only numbers to display"); 155 | fgets(input, 12, stdin); 156 | 157 | for (i = 0; i < strlen(input) - 1; i++) { // include the -1 other wize it will include the '\n' from fgets() 158 | 159 | //convert the string char eg '1' into the integer 1 to get the correct index in the numbers[] array 160 | // '0' starts a ascii 48 so subtract 48 to obtain the corrent int value 161 | int n = input[i] - 48; 162 | 163 | //print LCD number to stdout 164 | display(numbers[n],col); 165 | 166 | col = col + NUM_ROW + 1; 167 | } 168 | 169 | print_lcd(); 170 | 171 | return 0; 172 | } 173 | -------------------------------------------------------------------------------- /table-games/flightcrank.c: -------------------------------------------------------------------------------- 1 | //Author: flightcrank 2 | //Email: ocaumaxim@gmail.com 3 | 4 | #include 5 | #include 6 | 7 | #define MAX 101 8 | 9 | int max_word_length(char *str, char delim) { 10 | 11 | size_t num = strlen(str); 12 | int i; 13 | int max_count = 0; 14 | int count = 0; 15 | 16 | for (i = 0; i < num; i++) { 17 | 18 | if (str[i] == delim || str[i] == '\n') { 19 | 20 | if (count > max_count) { 21 | 22 | max_count = count; 23 | count = 0; //reset count 24 | continue; //skip delim char 25 | } 26 | 27 | //if the biggest word is in the middle if a list 28 | //the above if statement wont be met, so we must reset the count here too 29 | count = 0; //reset count 30 | } 31 | 32 | count++; 33 | } 34 | 35 | return max_count; 36 | } 37 | 38 | void table_end_caps(int max) { 39 | 40 | int i; 41 | 42 | printf("+"); 43 | 44 | for (i = 0; i < max + 2; i++) {// the 2 compinstes for the spaces added "+" and "+" 45 | 46 | printf("-"); 47 | } 48 | 49 | puts("+"); 50 | } 51 | 52 | void print_table(char *title, char *str, char delim) { 53 | 54 | unsigned int list_num = strlen(str); 55 | unsigned int title_num = strlen(title); 56 | int longest_word = max_word_length(str, ' '); 57 | int max = 0; 58 | int count = 0; 59 | int i; 60 | 61 | //replace the '\n' that fgets records with a null byte, so printf will print it without the '\n' that was there 62 | title[title_num -1] = 0; 63 | //update the title_num to refelct the above change 64 | title_num--; 65 | 66 | //determine the correct width of the table seeing what has the longest word 67 | //the title or a word in the list 68 | if (title_num < longest_word) { 69 | 70 | max = longest_word; 71 | 72 | } else { 73 | 74 | max = title_num; 75 | } 76 | 77 | //heading top 78 | table_end_caps(max); 79 | 80 | //heading title 81 | printf("| %s",title); 82 | 83 | for (i = title_num; i < max; i++) { 84 | 85 | printf(" "); 86 | } 87 | 88 | puts(" |"); 89 | 90 | //heading bottom 91 | table_end_caps(max); 92 | 93 | //data 94 | printf("| "); 95 | 96 | //loops through all chars in the entered string 97 | for (i = 0; i < list_num; i++) { 98 | 99 | //if we get to the delim char or the end of the string print out a newline 100 | if (str[i] == delim || str[i] == '\n') { 101 | 102 | //the current word will have the count var at its max value 103 | //by this point because the delimiter has been reached. 104 | //so figure out the number of spaces to print to have the 105 | //right side line match up to the longest word in the string 106 | if (count < max) { 107 | 108 | int diff = max - count; 109 | int j; 110 | 111 | for(j = 0; j < diff; j++) { 112 | 113 | printf(" "); 114 | } 115 | } 116 | 117 | //now we can print the line and a newline char, and start the next line off. 118 | if (str[i] == '\n') {//this is the last word in the list. just need a new line. 119 | 120 | printf(" |\n"); 121 | 122 | } else { 123 | 124 | printf(" |\n| "); 125 | } 126 | 127 | count = 0;//reset count for the next word 128 | continue;//we reached the delim, we dont want it added to a word so go back the 129 | //the start of the loop, which will i++ for us putting us to te start of the next word 130 | } 131 | 132 | //print out the char, and increase a counter that counts 133 | //the number of chars in the word 134 | printf("%c",str[i]); 135 | count++; 136 | } 137 | 138 | //data bottom 139 | table_end_caps(max); 140 | } 141 | 142 | int main() { 143 | 144 | char list[MAX]; 145 | char title[MAX]; 146 | 147 | puts("Enter a title for the list"); 148 | fgets(title, MAX, stdin); 149 | 150 | puts("Enter a list of words sepeated by spaces"); 151 | fgets(list, MAX, stdin); 152 | 153 | print_table(title, list, ' '); 154 | 155 | return 0; 156 | } 157 | -------------------------------------------------------------------------------- /wordsearch/flightcrank.c: -------------------------------------------------------------------------------- 1 | //Author: flightcrank 2 | //Email: ocaumaxim@gmail.com 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #define GRID 25 9 | #define MAX 100 10 | 11 | //create grid 12 | char word_grid[GRID][GRID]; 13 | 14 | void print_grid() { 15 | 16 | int i,j; 17 | 18 | for(i = 0; i < GRID; i++){ 19 | 20 | for(j = 0; j < GRID; j++) { 21 | 22 | printf("%c ",word_grid[i][j]); 23 | } 24 | 25 | printf("\n"); 26 | } 27 | } 28 | 29 | void fill_grid() { 30 | 31 | int i,j; 32 | 33 | for(i = 0; i < GRID; i++){ 34 | 35 | for(j = 0; j < GRID; j++) { 36 | 37 | if (word_grid[i][j] == ' ') { 38 | 39 | word_grid[i][j] = rand() % 26 + 97; 40 | } 41 | } 42 | 43 | } 44 | } 45 | 46 | int place_word_h(int x, int y, char word[], int len) { 47 | 48 | int i; 49 | int count = 0; 50 | 51 | //loop through each place in the word_grid 52 | //and place each char of the word in one space 53 | for (i = x; i < GRID; i++) { 54 | 55 | word_grid[y][i] = word[count]; 56 | count++; 57 | 58 | //exit function with success 59 | if (count > len) { 60 | return 0; 61 | } 62 | } 63 | 64 | 65 | return 1; 66 | } 67 | 68 | int place_word_v(int x, int y, char word[], int len) { 69 | 70 | int i; 71 | int count = 0; 72 | 73 | //loop through each place in the word_grid 74 | //and place each char of the word in one space 75 | for (i = y; i < GRID; i++) { 76 | 77 | word_grid[i][x] = word[count]; 78 | count++; 79 | 80 | //exit function with success 81 | if (count > len) { 82 | 83 | return 0; 84 | } 85 | } 86 | 87 | return 1; 88 | } 89 | 90 | //see if word will fit properly in the grid vertically 91 | int word_fit_v(int x, int y, char word[], int len) { 92 | 93 | int i; 94 | int count = 0; 95 | 96 | for (i = y; i < GRID; i++) { 97 | 98 | //loop through each place in the word_grid vertically 99 | if (word_grid[i][x] == ' ' || word_grid[i][x] == word[count]) { 100 | 101 | if (count == len) { 102 | 103 | return 0; 104 | 105 | } else { 106 | 107 | count++; 108 | } 109 | 110 | //cant place word there no empty space 111 | //or incompatible letter already in that space 112 | } else { 113 | 114 | return 1; 115 | } 116 | } 117 | 118 | 119 | return 1; 120 | } 121 | 122 | //see if word will fit properly in the grid horizontally 123 | int word_fit_h(int x, int y, char word[], int len) { 124 | 125 | int i; 126 | int count = 0; 127 | 128 | //loop through each place in the word_grid horizontally 129 | for (i = x; i < GRID; i++) { 130 | 131 | //if the space in the grid is empty 132 | //or 133 | //if it has the same letter already in it. 134 | if(word_grid[y][i] == ' ' || word_grid[y][i] == word[count]) { 135 | 136 | if (count == len) { 137 | 138 | return 0; 139 | 140 | } else { 141 | 142 | count++; 143 | } 144 | //cant place word there no empty space 145 | //or incompatible letter already in that space 146 | } else { 147 | 148 | return 1; 149 | } 150 | } 151 | 152 | 153 | return 1; 154 | } 155 | 156 | int main() { 157 | 158 | int i,j; 159 | char line[MAX]; 160 | 161 | //populate grid 162 | for(i = 0; i < GRID; i++){ 163 | 164 | for(j = 0; j < GRID; j++) { 165 | 166 | word_grid[i][j] = ' '; 167 | } 168 | } 169 | 170 | //open file to process 171 | FILE *fp; 172 | fp = fopen("words.txt","r"); 173 | 174 | if (fp == NULL) { 175 | 176 | printf("error opening file. does pt.txt exist ?"); 177 | return 1; // exit program 178 | } 179 | 180 | //loop throughout each line in the file. 181 | while (fgets(line, MAX - 1, fp) != NULL) { 182 | 183 | int len; 184 | int rx; 185 | int ry; 186 | int r; 187 | 188 | len = strlen(line); 189 | 190 | //strlen includes the null byte 191 | //we only need exact letters of out word 192 | if (line[len - 1] == '\n') { 193 | 194 | line[len -1] = 0; //replace '\n' with null for better printing to stdout 195 | len -= 2;//compensate for original nullbyte and '\n' 196 | 197 | } else { 198 | 199 | len--; 200 | } 201 | 202 | //attempt to randomly place word in the grid. 203 | while (1) { 204 | 205 | r = random() % 2; 206 | rx = rand() % GRID; 207 | ry = rand() % GRID; 208 | 209 | //horizontal 210 | if (r == 0) { 211 | 212 | //will fit the grid left to right 213 | if(len < GRID - rx) { 214 | 215 | if (word_fit_h(rx,ry,line,len) == 0) { 216 | 217 | place_word_h(rx,ry,line,len); 218 | break; 219 | } 220 | } 221 | 222 | //vertical 223 | } else { 224 | 225 | //will fit in grid top down 226 | if (len + ry < GRID) { 227 | 228 | if (word_fit_v(rx, ry, line, len) == 0) { 229 | 230 | place_word_v(rx,ry,line,len); 231 | break; 232 | } 233 | } 234 | } 235 | } 236 | 237 | printf("%s\n",line); 238 | } 239 | 240 | //close file 241 | fclose(fp); 242 | 243 | print_grid();//print out grid before the junk letters are printed 244 | printf("\n"); 245 | fill_grid(); 246 | printf("\n"); 247 | print_grid(); 248 | 249 | return 0; 250 | } 251 | -------------------------------------------------------------------------------- /working-with-time/flightcrank.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #define DAY 13 5 | #define MONTH 10 6 | #define YEAR 2012 7 | 8 | enum months {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; 9 | 10 | int is_leap(int year) { 11 | 12 | if (year % 4 == 0) { 13 | 14 | if (year % 400 == 0) { 15 | 16 | return 1; 17 | } 18 | 19 | if (year % 100 == 0) { 20 | 21 | return 0; 22 | } 23 | 24 | return 1; 25 | } 26 | 27 | return 0; 28 | } 29 | 30 | int leap_num(int year) { 31 | 32 | int i; 33 | int total = 0; 34 | 35 | 36 | for (i = 1; i <= year; i++) { 37 | 38 | if (is_leap(i)) { 39 | 40 | total++; 41 | } 42 | } 43 | 44 | return total; 45 | } 46 | 47 | int main() { 48 | 49 | int leap = leap_num(YEAR - 1); 50 | int non_leap = (YEAR - 1) - leap; 51 | int days = 0; 52 | 53 | //jan 54 | days += (YEAR - 1) * 31; 55 | 56 | //feb 57 | days += (leap * 29) + (non_leap * 28); 58 | 59 | //mar 60 | days += (YEAR - 1) * 31; 61 | 62 | //apr 63 | days += (YEAR - 1) * 30; 64 | 65 | //may 66 | days += (YEAR - 1) * 31; 67 | 68 | //jun 69 | days += (YEAR - 1) * 30; 70 | 71 | //jul 72 | days += (YEAR - 1) * 31; 73 | 74 | //aug 75 | days += (YEAR - 1) * 31; 76 | 77 | //sep 78 | days += (YEAR - 1) * 30; 79 | 80 | //oct 81 | days += (YEAR - 1) * 31; 82 | 83 | //nov 84 | days += (YEAR - 1) * 30; 85 | 86 | //dec 87 | days += (YEAR - 1) * 31; 88 | 89 | 90 | switch (MONTH - 1) { 91 | 92 | case JAN: 93 | 94 | days += DAY; 95 | break; 96 | 97 | case FEB: 98 | 99 | days += 31;//jan 100 | days += DAY; 101 | break; 102 | 103 | case MAR: 104 | 105 | days += 31;//jan 106 | 107 | //feb 108 | if (is_leap(YEAR)){ 109 | 110 | days += 29; 111 | } else { 112 | 113 | days += 28; 114 | } 115 | 116 | days += DAY; 117 | break; 118 | 119 | case APR: 120 | 121 | days += 31;//jan 122 | 123 | //feb 124 | if (is_leap(YEAR)){ 125 | 126 | days += 29; 127 | } else { 128 | 129 | days += 28; 130 | } 131 | 132 | days += 31;//mar 133 | days += DAY; 134 | break; 135 | 136 | case MAY: 137 | 138 | days += 31;//jan 139 | 140 | //feb 141 | if (is_leap(YEAR)) { 142 | 143 | days += 29; 144 | 145 | } else { 146 | 147 | days += 28; 148 | } 149 | 150 | days += 31;//mar 151 | days += 30;//apr 152 | days += DAY; 153 | break; 154 | 155 | case JUN: 156 | 157 | days += 31;//jan 158 | 159 | //feb 160 | if (is_leap(YEAR)) { 161 | 162 | days += 29; 163 | 164 | } else { 165 | 166 | days += 28; 167 | } 168 | 169 | days += 31;//mar 170 | days += 30;//apr 171 | days += 31;//may 172 | days += DAY; 173 | break; 174 | 175 | case JUL: 176 | 177 | days += 31;//jan 178 | 179 | //feb 180 | if (is_leap(YEAR)) { 181 | 182 | days += 29; 183 | 184 | } else { 185 | 186 | days += 28; 187 | } 188 | 189 | days += 31;//mar 190 | days += 30;//apr 191 | days += 31;//may 192 | days += 30;//jun 193 | days += DAY; 194 | break; 195 | 196 | case AUG: 197 | 198 | days += 31;//jan 199 | 200 | //feb 201 | if (is_leap(YEAR)) { 202 | 203 | days += 29; 204 | 205 | } else { 206 | 207 | days += 28; 208 | } 209 | 210 | days += 31;//mar 211 | days += 30;//apr 212 | days += 31;//may 213 | days += 30;//jun 214 | days += 31;//jul 215 | days += DAY; 216 | break; 217 | 218 | case SEP: 219 | 220 | days += 31;//jan 221 | 222 | //feb 223 | if (is_leap(YEAR)) { 224 | 225 | days += 29; 226 | 227 | } else { 228 | 229 | days += 28; 230 | } 231 | 232 | days += 31;//mar 233 | days += 30;//apr 234 | days += 31;//may 235 | days += 30;//jun 236 | days += 31;//jul 237 | days += 31;//aug 238 | days += DAY; 239 | break; 240 | 241 | case OCT: 242 | days += 31;//jan 243 | 244 | //feb 245 | if (is_leap(YEAR)) { 246 | 247 | days += 29; 248 | 249 | } else { 250 | 251 | days += 28; 252 | } 253 | 254 | days += 31;//mar 255 | days += 30;//apr 256 | days += 31;//may 257 | days += 30;//jun 258 | days += 31;//jul 259 | days += 31;//aug 260 | days += 30;//sep 261 | days += DAY; 262 | break; 263 | 264 | case NOV: 265 | days += 31;//jan 266 | 267 | //feb 268 | if (is_leap(YEAR)) { 269 | 270 | days += 29; 271 | 272 | } else { 273 | 274 | days += 28; 275 | } 276 | 277 | days += 31;//mar 278 | days += 30;//apr 279 | days += 31;//may 280 | days += 30;//jun 281 | days += 31;//jul 282 | days += 31;//aug 283 | days += 30;//sep 284 | days += 31;//oct 285 | days += DAY; 286 | 287 | break; 288 | 289 | case DEC: 290 | 291 | days += 31;//jan 292 | 293 | //feb 294 | if (is_leap(YEAR)) { 295 | 296 | days += 29; 297 | 298 | } else { 299 | 300 | days += 28; 301 | } 302 | 303 | days += 31;//mar 304 | days += 30;//apr 305 | days += 31;//may 306 | days += 30;//jun 307 | days += 31;//jul 308 | days += 31;//aug 309 | days += 30;//sep 310 | days += 31;//oct 311 | days += 30;//nov 312 | days += DAY; 313 | 314 | break; 315 | 316 | } 317 | 318 | int d = days % 7; 319 | 320 | switch (d) { 321 | 322 | case 1: 323 | puts("monday"); 324 | break; 325 | 326 | case 2: 327 | puts("tuesday"); 328 | break; 329 | 330 | case 3: 331 | puts("wednesday"); 332 | break; 333 | 334 | case 4: 335 | puts("thursday"); 336 | break; 337 | 338 | case 5: 339 | puts("friday"); 340 | break; 341 | 342 | case 6: 343 | puts("saturday"); 344 | break; 345 | 346 | case 0: 347 | puts("sunday"); 348 | break; 349 | } 350 | 351 | return 0; 352 | } 353 | 354 | --------------------------------------------------------------------------------