Jump to content

The under 100 line challenge!

fletch to 99

I made Tic Tac Toe in C++ w/ 99 lines.

#include <iostream>#include <string>using namespace std;int main() {	//Set up the board (2D array of chars)	char board[3][3] = { { ' ', ' ', ' ' },						 { ' ', ' ', ' ' },						 { ' ', ' ', ' ' } };	//Instantiate variables	bool xTurn = true;	char currentLetter;	int selectedPosition;	//Output the board	for (int i = 0; i < 3; i++)	{		for (int j = 0; i < 3; i++)		{			cout << board[i][j];		}		cout << endl;	}	//Start the turn	//use a loop for each turn, 9 turns max	for (int i = 0; i < 9; i++){		//Determine current player		//Accept their input (the spot that they choose)		if (xTurn){			cout << "Player 1's Turn, enter 1-9 for box.";			cin >> selectedPosition;			currentLetter = 'x';		}		else{			cout << "Player 2's Turn, enter 1-9 for box.";			cin >> selectedPosition;			currentLetter = 'o';		}		if (selectedPosition == 1 && board[0][0] == ' ')			board[0][0] = currentLetter;		else if (selectedPosition == 2 && board[0][1] == ' ')			board[0][1] = currentLetter;		else if (selectedPosition == 3 && board[0][2] == ' ')			board[0][2] = currentLetter;		else if (selectedPosition == 4 && board[1][0] == ' ')			board[1][0] = currentLetter;		else if (selectedPosition == 5 && board[1][1] == ' ')			board[1][1] = currentLetter;		else if (selectedPosition == 6 && board[1][2] == ' ')			board[1][2] = currentLetter;		else if (selectedPosition == 7 && board[2][0] == ' ')			board[2][0] = currentLetter;		else if (selectedPosition == 8 && board[2][1] == ' ')			board[2][1] = currentLetter;		else if (selectedPosition == 9 && board[2][2] == ' ')			board[2][2] = currentLetter;		//Place either an x or an o in the spot		//depending on which player is active		//Output the board		for (int i = 0; i < 3; i++)		{			for (int j = 0; j < 3; j++)			{				cout << board[i][j];			}			cout << endl;		}		//Check for victory		//Conditional statements		if (board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][2] != ' ' || board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][2] != ' ' || board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[2][2] != ' '){			cout << currentLetter << " won the game!" << endl; //up and down			return 0;		}		else if (board[0][0] == board[1][0] && board[1][0] == board[2][0] && board[2][0] != ' ' || board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[2][1] != ' ' || board[0][2] == board[1][2] && board[1][2] == board[2][2] && board[2][2] != ' '){			cout << currentLetter << " won the game!" << endl; //across			return 0;		}		else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] != ' ' || board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] != ' '){			cout << endl << currentLetter << " won the game!" << endl << endl; //diagnol			return 0;		}				//End the turn		xTurn = !xTurn;	}	cout << "It's a tie!";	return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

Here's a rather simple cypher :

#include <cstring>#include <ctype.h>int get_position_alpha(char c){    if(!isalpha©) return -1;    else    {        if(islower©) return c - 'a'+1;        if(isupper©) return c - 'A'+1;        return -1;    }}int abs(int n){    if(n >= 0) return n;    else return -1*n;}void cypher(char *str, int len, int offset){    for(int i = 0; i < len; i++)    {        if(*(str+i) == ' ') ;        else        {            if(!isalpha(*(str+i)));            else            {                char result = *(str+i) + offset;                if(islower(*(str+i)) && result <'a')                {                    result += int('z') - int('a') - 1;                }                else if(islower(*(str+i)) && result > 'z')                {                    result += int('a') - 1 - int('z');                }                else if(isupper(*(str+i)) && result < 'A')                {                    result += int('Z') - int('A') - 1;                }                else if(isupper(*(str+i)) && result > 'Z')                {                    result += int('A') - 1 - int('Z');                }                *(str+i) = result;            }        }    }}void cypher(char *str, int offset){    cypher(str, (int)strlen(str) , offset);}void decypher(char *str, int offset){    cypher(str, -1*offset);}void cypher(char *str, int *key, int key_len){    char *begin = str;    for(int i = 0; i <key_len && *(begin) != '\0'; i++)    {        cypher(begin,abs(*(key+i)), *(key+i));        begin += abs(*(key+i));    }}void cypher(char *str, char *Key){    int len = strlen(Key);    int key[len];    for(int i = 0; i < len; i++)        key[i] = get_position_alpha(*(Key+i));    cypher(str, key, len);}void decypher(char *str, char *Key){    int len = strlen(Key);    int key[len];    for(int i = 0; i < len; i++)        key[i] = -1*get_position_alpha(*(Key+i));    cypher(str, key, len);}

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

hehe, nearly any c/c++ program can be squeezed into a single line ;)

 

I'd fail that on it being a formatting mess

CPU: Intel Core i7-4770k | Mobo: MSI Mpower Max | Cooling: Cryorig R1 Ultimate w/ XT140 front Fan | GPU: EVGA GTX 770 Dual SC SLI | Case: NZXT H440 | Case Fans: Phanteks PH-140SP x5 | PSU: EVGA Supernova P2 1000W | RAM: 16GB Crucial Ballistix Tactical Tracer | SSD: Kingston HyperX 3k 120GB | HDD: Seagate Barracude

Keyboard: Razer Blackwidow Ultimate 2013 | Mouse: Razer Deathadder 2013 | Headphones: Sennheiser HD438s | Mousepad: Razer Goliathus Control | Monitor 1: Benq XL2430T | Monitor 2: BenQ RL2455HM 

 

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

although i like the idea. do we keep copyright?

GPL or Apache is preferred

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

Text editor in C++:

#include <iostream>#include <fstream>#include <string>#include <vector>using namespace std;fstream file;vector<string> v(0);void help(){    cout<<"This is a text editor written in C++.\nYou can open it with the command : editor.exe <file_to_open>./nUsage : \np - Print everything in the file.\np <line_nr> - Print everything in the file found at line nr <line_nr>.\nw <line_nr> <string_to_write> - write <string_to_write> at line nr <line_nr>.\n";}void print(){    for(vector<string>::iterator it = v.begin(); it!=v.end(); it++)cout << *it << '\n';}void printl(int line){    if(line < v.size())        cout << v[line]<<'\n';    else cout << "Line not found.\n";}int main(int argc, char *argv[]){    switch(argc)    {    case 1 :        help();        break;    case 2:    {        file.open(argv[1], ios::in | ios::out);        string str;        while(getline(file,str))v.push_back(str);        str.clear();        file.close();        bool running = 1;        while(running)        {            getline(cin,str);            if(str[0]=='p' && str.length() < 2) print();            else if(str[0]=='p')            {                int line = (int)str[2]-48;                printl(line);            }            else if(str[0]=='w' && str.length() > 4)            {                int line = (int)str[2]-48;                string s = str.substr(4);                v[line] = s;            }            else if(str[0]=='s')            {                ofstream out(argv[1]);                for(vector<string>::iterator it = v.begin(); it!=v.end(); it++)out << *it << '\n';                out.close();            }            else if(str[0]=='q')            {                running = 0;                return 0;            }            else help();        }    }    break;    default :        return 0;    }}

You can open it with the command : editor.exe <file_to_open>.

Usage :
p - Print everything in the file.
p <line_nr> - Print everything in the file found at line nr <line_nr>.
w <line_nr> <string_to_write> - write <string_to_write> at line nr <line_nr>.

.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

A simple Resistance Calculator. Built using C# through the ide Monodevelop.

using System;namespace resistorCalc{	class MainClass	{		public static void Main (string[] args)		{			//bool continue1 = true;			decimal sourceVolts;			decimal outVolts;			decimal current;			decimal resistance;			string continue2;			for (bool continue1 = true; continue1 == true;){			Console.WriteLine ("Resistance Calc");				Console.WriteLine ("Enter your source Voltage.");				sourceVolts = Convert.ToDecimal(Console.ReadLine());				Console.WriteLine ("Enter your desired output voltage.");				outVolts = Convert.ToDecimal (Console.ReadLine ());				Console.WriteLine ("Enter your output's required current. In amps.");				current = Convert.ToDecimal (Console.ReadLine ());				resistance = (sourceVolts - outVolts) / (current / 1000);				Console.WriteLine ("Your required resistance is " + resistance + "ohms, or close to that.");				Console.WriteLine ("Continue?");				continue2 = Console.ReadLine();				switch (continue2) {				case "yes":					continue1 = true;					break;				case "no":					continue1 = false;					break;				default:					continue1 = true;					break;				}			}		}	}}

Case: NZXT Phantom Green Trim   Graphics Card: EVGA GTX 670 2gb FTW    CPU: Intel i7 4770k 3.5GHZ    Motherboard: Gigabyte G1 Sniper 5   Hard Drives: One Western Digital 1TB & Hitachi 1TB   Power Supply: Corsair RM 850w

 

Cooler: Corsair H80i Liquid Cooler    Ram: Corsair Vengeance 2x4gb & Mushkin 1x4gb   Monitors: Two Asus 22'' 1080p monitors   Keyboard: CM Storm Quickfire XT Green   Mouse: Razer Naga 2014   Microphone:  Audio- Technica AT2020 XLR with a Alesis MultiMix 4   Headphones: Sennheiser HD 280 Pro

Link to comment
Share on other sites

Link to post
Share on other sites

I'm making a program to automatically set up the Windows 10 privacy settings. It's functional, but not feature complete!

Github link

http://bgr.com/2015/08/14/windows-10-spying-prevention-privacy-tools/#

 

already quite a few about

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

This was right on the release of Win 10, not 2 months in.

sorry did not read the date :P did you ever finish it?

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

sorry did not read the date :P did you ever finish it?

It's sort of done, only the stuff you can change in the registry though.

Link to comment
Share on other sites

Link to post
Share on other sites

 

A simple Resistance Calculator. Built using C# through the ide Monodevelop.

 

 

you can knock a few more lines off, hope you don't mind

using System;namespace Loop{    class Program    {        static void Main(string[] args)        {                        decimal sourceVolts;            decimal outVolts;            decimal current;            decimal resistance;            string cont;            do            {                Console.WriteLine("Resistance Calc\nEnter your source Voltage.");                                sourceVolts = Convert.ToDecimal(Console.ReadLine());                Console.WriteLine("Enter your desired output voltage.");                outVolts = Convert.ToDecimal(Console.ReadLine());                Console.WriteLine("Enter your output's required current. In amps.");                current = Convert.ToDecimal(Console.ReadLine());                resistance = (sourceVolts - outVolts) / (current / 1000);                Console.WriteLine("Your required resistance is " + resistance + "ohms, or close to that.");                Console.WriteLine("Continue? (y or n)");                cont = Console.ReadLine();                cont = cont.ToLower();                switch (cont)                {                    case "n":                                                break;                }                            } while (true);        }    }}

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Hangman for PowerShell. You can goof off whenever you want now. (The school left PowerShell unlocked, bad idea...)

EDIT: Split it out, exactly 100 lines.

function hangman($word){	[system.reflection.assembly]::loadwithpartialname("System.Windows.Forms");	[System.Windows.Forms.SendKeys]::Sendwait('%{F7 2}');	$cheat=$false;	$word=$word.ToUpper();	$wordfield="";	for($length=0;$length-lt$word.Length;$length++){		if($word.Substring($length,1)-like" "){			$wordfield+=" ";		}else{			$wordfield+="_";		}	}	clear;	Write-Host ("Welcome to Hangman for PowerShell. You have 6 failures to guess the word (or possibly phrase) your friend set for you. The word/phrase is "+$word.Length+" characters long. Good luck!");	if((Read-Host "Press enter to continue...")-like"cheatmode"){		$cheat=$true;	}	clear;	Write-Host "Start!";	drawdeadman($wrongletters);	Write-Host ("Incorrect Letters: "+$wrongletters+". Current word: "+$wordfield);	$word=$word.ToUpper();	$guess="";	$wrongletters="";	while($true){		$guess=(Read-Host "Guess a letter, any letter").ToUpper();		clear;		if($wrongletters.Contains($guess)-Or$wordfield.Contains($guess)){			Write-Host "You already tried that."		}		if(-Not($word.Contains($guess))-And-Not($wrongletters.Contains($guess))){			Write-Host "Wrong.";			$wrongletters+=$guess;		}		if($word.Contains($guess)-And-Not($wrongletters.Contains($guess))-And-Not($wordfield.Contains($guess))){			Write-Host "Yes!";			for($field=0;$field-le($word.Length-1);$field++){				if($word.Substring($field,1)-like$guess){					$sub1=$wordfield.Substring(0,$field);					$sub2=$wordfield.Substring($field+1,$word.Length-($field+1));					$wordfield=($sub1+$guess+$sub2);				}			}		}		drawdeadman($wrongletters);		if($wrongletters.Length-ge6){			$wrongletters="";			$wordfield="";			Write-Host ("You failed! The word/phrase was "+$word+".");			return;		}		if($wordfield.Contains($word)-Or($guess-like$word)-Or($cheat-like$true)){			$wrongletters="";			$wordfield="";			Write-Host ("You Won! The word/phrase was "+$word);			$cheat=$false;			return;		}		Write-Host ("Incorrect Letters: "+$wrongletters+" Current word: "+$wordfield);$guess="";	}}function drawdeadman($wrongletter){	Write-Host "  ___";	if($wrongletters.Length-eq0){		Write-Host "  |";		Write-Host "  |";		Write-Host "  |";	}elseif($wrongletters.Length-eq1){		Write-Host "  |  O";		Write-Host "  |";		Write-Host "  |";	}elseif($wrongletters.Length-eq2){		Write-Host "  |  O";		Write-Host "  |  |";		Write-Host "  |";	}elseif($wrongletters.Length-eq3){		Write-Host "  |  O";		Write-Host "  | /|";		Write-Host "  |";	}elseif($wrongletters.Length-eq4){		Write-Host "  |  O";		Write-Host "  | /|\";		Write-Host "  |";	}elseif($wrongletters.Length-eq5){		Write-Host "  |  O";		Write-Host "  | /|\";		Write-Host "  | /";	}elseif($wrongletters.Length-eq6){		Write-Host "  |  O";		Write-Host "  | /|\";		Write-Host "  | / \";	}	Write-Host " _|_____";	Write-Host "/       \";}

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
Share on other sites

Link to post
Share on other sites

 

you can knock a few more lines off, hope you don't mind

using System;namespace Loop{    class Program    {        static void Main(string[] args)        {                        decimal sourceVolts;            decimal outVolts;            decimal current;            decimal resistance;            string cont;            do            {                Console.WriteLine("Resistance Calc\nEnter your source Voltage.");                                sourceVolts = Convert.ToDecimal(Console.ReadLine());                Console.WriteLine("Enter your desired output voltage.");                outVolts = Convert.ToDecimal(Console.ReadLine());                Console.WriteLine("Enter your output's required current. In amps.");                current = Convert.ToDecimal(Console.ReadLine());                resistance = (sourceVolts - outVolts) / (current / 1000);                Console.WriteLine("Your required resistance is " + resistance + "ohms, or close to that.");                Console.WriteLine("Continue? (y or n)");                cont = Console.ReadLine();                cont = cont.ToLower();                switch (cont)                {                    case "n":                                                break;                }                            } while (true);        }    }}

Except the switch statement won't end the program. I had to change it slightly so the while loop uses a boolean variable which the switch statement changes in order to end the program.

 

Edit: Just rewrote the code to be in c++. https://github.com/rommac100/ResistanceCalc

Case: NZXT Phantom Green Trim   Graphics Card: EVGA GTX 670 2gb FTW    CPU: Intel i7 4770k 3.5GHZ    Motherboard: Gigabyte G1 Sniper 5   Hard Drives: One Western Digital 1TB & Hitachi 1TB   Power Supply: Corsair RM 850w

 

Cooler: Corsair H80i Liquid Cooler    Ram: Corsair Vengeance 2x4gb & Mushkin 1x4gb   Monitors: Two Asus 22'' 1080p monitors   Keyboard: CM Storm Quickfire XT Green   Mouse: Razer Naga 2014   Microphone:  Audio- Technica AT2020 XLR with a Alesis MultiMix 4   Headphones: Sennheiser HD 280 Pro

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

 
------------{asks user for number}-------------print("type a number")      local input = tonumber(io.read())----------{split number into digits within a table}----------- local Digit = {tonumber(string.sub(input, 1, 1)), tonumber(string.sub(input, 2, 2)), tonumber(string.sub(input, 3, 3))}local length = string.len(input)--------------{convert to number}--------------     local lengthNumber = tonumber(length)-----------{writing out the number}------------if lengthNumber == 1 thenif tonumber(input) == 1 thenio.write("One\n")endif tonumber(input) == 2 thenio.write("Two\n")endif tonumber(input) == 3 thenio.write("Three\n")endif tonumber(input) == 4 thenio.write("Four\n")endif tonumber(input) == 5 thenio.write("Five\n")endif tonumber(input) == 6 thenio.write("Six\n")endif tonumber(input) == 7 thenio.write("Seven\n")endif tonumber(input) == 8 thenio.write("Eight\n")endif tonumber(input) == 9 thenio.write("Nine\n")endendif (lengthNumber == 2 and Digit[1] == 1) thenif Digit[2] == 0 thenio.write("Ten\n")endif Digit[2] == 1 thenio.write("Eleven\n")endif Digit[2] == 2 thenio.write("Twelve\n")endif Digit[2] == 3 thenio.write("Thirteen\n")endif Digit[2] == 4 thenio.write("Fourteen\n")endif Digit[2] == 5 thenio.write("Fifteen\n")endif Digit[2] == 6 thenio.write("Sixteen\n")endif Digit[2] == 7 thenio.write("Seventeen\n")endif Digit[2] == 8 thenio.write("Eighteen\n")endif Digit[2] == 9 thenio.write("Nineteen\n")endendif (lengthNumber == 2 and Digit[1] == 2) thenio.write("Twenty")if Digit[2] == 1 thenio.write("One\n")endif Digit[2] == 2 thenio.write("Two\n")endif Digit[2] == 3 thenio.write("Three\n")endif Digit[2] == 4 thenio.write("Four\n")endif Digit[2] == 5 thenio.write("Five\n")endif Digit[2] == 6 thenio.write("Six\n")endif Digit[2] == 7 thenio.write("Seven\n")endif Digit[2] == 8 thenio.write("Eight\n")endif Digit[2] == 9 thenio.write("Nine\n")endend
 

LUA

 

this is my first real world application program, however it is not finished it only accepts integers 1-29., it probably could be under 100 lines, but i like to make my code readable. had trouble with the if, else, elseif and a few other small mistakes, but my friend Austin helped me out like the good friend that he is

 

I'm going to enlighten you on something called for-loops. They're great and makes 99% of that code unneccessary.

http://www.lua.org/pil/4.3.4.html

 

Also, I recommend not to start programming in Lua. I'm assuming you're using it because of ComputerCraft but try to for example write a simple mod instead.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm going to enlighten you on something called for-loops. They're great and makes 99% of that code unneccessary.

http://www.lua.org/pil/4.3.4.html

 

Also, I recommend not to start programming in Lua. I'm assuming you're using it because of ComputerCraft but try to for example write a simple mod instead.

 

Even if he did a loop he'd still need to do all of the if statements, the best way would be to use an array.

Link to comment
Share on other sites

Link to post
Share on other sites

Not sure if you are allowed to post multiple applications you have written but I will do so. Here is a c++ calculator, that does allows as many numbers as needed. https://github.com/rommac100/calcCPlusPlus/blob/master/main.cpp Still working on it. But I think it isn't to shabby.

Case: NZXT Phantom Green Trim   Graphics Card: EVGA GTX 670 2gb FTW    CPU: Intel i7 4770k 3.5GHZ    Motherboard: Gigabyte G1 Sniper 5   Hard Drives: One Western Digital 1TB & Hitachi 1TB   Power Supply: Corsair RM 850w

 

Cooler: Corsair H80i Liquid Cooler    Ram: Corsair Vengeance 2x4gb & Mushkin 1x4gb   Monitors: Two Asus 22'' 1080p monitors   Keyboard: CM Storm Quickfire XT Green   Mouse: Razer Naga 2014   Microphone:  Audio- Technica AT2020 XLR with a Alesis MultiMix 4   Headphones: Sennheiser HD 280 Pro

Link to comment
Share on other sites

Link to post
Share on other sites

Calculates a weighted grade average, with separate categories.

I'm still pretty new, so I don't know if this is well formatted or not. I do have comments and everything is well spaced out (under 70 lines). 

 

http://pastebin.com/4fZ2mXub

May Our Framerates Be High And Our Temperatures Be Low.

PSUs: EVGA B2/G2/GQ/GS, Corsair RMx/i, Grey-Label CXM, Everything Seasonic/Delta/Super Flower, XFX except XT

Use pcpartpicker.com

Link to comment
Share on other sites

Link to post
Share on other sites

  • 3 weeks later...

I'm going to enlighten you on something called for-loops. They're great and makes 99% of that code unneccessary.

http://www.lua.org/pil/4.3.4.html

 

Also, I recommend not to start programming in Lua. I'm assuming you're using it because of ComputerCraft but try to for example write a simple mod instead.

yea, when I wrote this code, I was really new to coding. Thanks for the tip, but a bit late, im much more advanced now. I am not familiar with "ComputerCraft", a friend of mine introduced me to Lua, I asked for a practice problem, this was my solution at the time.

Energy can not be created nor destroyed, just converted to a different Type of energy.
My blog: T3Krant.blogspot.com

Link to comment
Share on other sites

Link to post
Share on other sites

I'm trying to get into programming, and I just had a question.

 

What language should I use? I started using C#, but I sort of bailed on it after I learned how to write things like

 

Console.WriteLine("How old are you?"

userValue = Console.ReadLine();

 

string = message "";

 

if (userValue == "17")

   message = "You're a loser";

 

Console.WriteLine(message);

Console.ReadLine();

 

 

w/e. I just want the ability to make basic applications like some of these. Where is the best place to start?

Link to comment
Share on other sites

Link to post
Share on other sites

Okay so here is my submission. This is written in OCaml (functional language) which allows a different style of coding and the ability for short concise instructions (24 lines)

What the program does:

This is a function that sorts a list of non-empty lists increasingly according to the maximum element in the list.

e.g.

[ [7;6;5;4]; [4;3;2]; [1; 2; 3] ]   =   [ [1;2;3]; [4;3;2]; [7;6;5;4] ]

so 3 is the max element in the first list, 4 is the max element in the second, 7 is the max in the third and so on...

 

let rec matchlst l1 l2 = match l1,l2 with   | [],[] -> true   | [],_ -> false   | _,[] -> false   | hd1::tl1,hd2::tl2 -> if hd1=hd2 then matchlst tl1 tl2 else false let rec largestSingle lst max = match lst with   | [] -> max   | hd::tl -> if hd>max then largestSingle tl hd else largestSingle tl max let rec delete elem list = match list with   | [] -> []   | hd::tl -> if(matchlst hd elem) then tl else hd::(delete elem tl) let rec largest lst largeSing largeList = match lst with   | [] -> largeList   | hd::tl -> match hd with      | [] -> largest tl largeSing largeList      | hd'::tl' -> if (largestSingle hd min_int) >= largeSing then largest tl (largestSingle hd min_int) hd else largest tl largeSing largeListlet rec sortll lst = match lst with   | [] -> []   | hd::tl -> let max = (largest lst min_int []) in      (sortll (delete max lst))@([max])
http://try.ocamlpro.com/

^ an online parser for those interested.

Thanks for reading

Ooooh, I'm gonna check this out.

#!/bin/bashscripts = 10stillHasCode = falseif (stillHasCode = false) then{    echo "Oh dear, I had" + $scripts + "now I have none. :("    scripts = 0}exit 

Sadly, the rig I was using died, as did my backups. I had a neat (but redundant) little BGM and SFX management library I wrote for the Spherical Game Engine. (Which is open source, of course) It's based on javascript.

 

I also had a few scripts written in Python, BASH, and BATCH but those went out the window along with my spherical backups.

 

So for now, have a look at my experiments with Scratch (A great tool for introducing programming)

https://scratch.mit.edu/projects/75498498/#editor

My procrastination is the bane of my existence.

I make games and stuff in my spare time.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

  • 4 weeks later...

Don't know how useful you'd consider this, but here's an implementation of Bogosort written in Java just for fun, https://en.wikipedia.org/wiki/Bogosort. It's absolutely horrible, but it does sort the list  ;). Lol I'd love to write Bogobogosort, guaranteed to only complete after the inevitable heat death of the universe :D.

 

The program takes a list of space delimited integer arguments when called and sorts the list and comes in at 54 lines, but could definitely be shorter.

import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Random;public class Bogosort {		public static void main(String[] args){		ArrayList<Integer> list = new ArrayList<Integer>();		for(String s : args){			list.add(Integer.parseInt(s));		}		while(!isSorted(list)){			System.out.print("Unsorted list: ");			print(list);			shuffle(list);					}				System.out.print("Sorted list:   ");		print(list);			}		public static boolean isSorted(ArrayList<Integer> list){		int previous = list.get(0);		for(int i = 1; i < list.size(); i++){			if(previous <= list.get(i)){				previous = list.get(i);			} else {				return false;			}		}		return true;				}		public static void shuffle(ArrayList<Integer> list){		Collections.sort(list, new Comparator<Integer>() {			public int compare(Integer o1, Integer o2) {				Random r = new Random();				return r.nextInt(2) - 1;			}		});	}		public static void print(ArrayList<Integer> list){		for(int i = 0; i < list.size(); i++){			if(i < list.size() - 1)				System.out.print(list.get(i) + ",");			else				System.out.print(list.get(i));		}		System.out.println();	}}
Link to comment
Share on other sites

Link to post
Share on other sites

Hmmm, can I lift part of the sudoku solver I'm working on to dump in here. Maybe challenge myself to do it in 100 line blocks

CPU: Intel Core i7-4770k | Mobo: MSI Mpower Max | Cooling: Cryorig R1 Ultimate w/ XT140 front Fan | GPU: EVGA GTX 770 Dual SC SLI | Case: NZXT H440 | Case Fans: Phanteks PH-140SP x5 | PSU: EVGA Supernova P2 1000W | RAM: 16GB Crucial Ballistix Tactical Tracer | SSD: Kingston HyperX 3k 120GB | HDD: Seagate Barracude

Keyboard: Razer Blackwidow Ultimate 2013 | Mouse: Razer Deathadder 2013 | Headphones: Sennheiser HD438s | Mousepad: Razer Goliathus Control | Monitor 1: Benq XL2430T | Monitor 2: BenQ RL2455HM 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×