Skip to content

Instantly share code, notes, and snippets.

@chestergrant
chestergrant / Hillclimbing_1.py
Last active August 29, 2015 14:22
Hillclimbing algorithm for Travelling saleman problem
import math
import random
#Prints the solution path
def print_solution(solution,distances, cities):
print "Path : "+cities[solution[0]]+"-->"+cities[solution[1]]+"-->"+cities[solution[2]]+"-->"+cities[solution[3]]+"-->"+cities[solution[4]]+"-->"+cities[solution[5]]
print "Total Distance: "+str(solution_value(solution,distances,cities))
#Generate a random path through the cities
def random_solution():
@chestergrant
chestergrant / hillclimbing.py
Last active August 29, 2015 14:04
Search through the character space to find your name
import math
import random
#Randomly modifies part of a possible solution
def modify_solution(solution,characters):
solution_idx = random.randint(0,len(solution)-1)
character_idx = random.randint(0,len(characters)-1)
output = copy_solution(solution)
output[solution_idx] = characters[character_idx]
return output
@chestergrant
chestergrant / InterweavingNames.java
Created July 1, 2012 08:04
Print my first name 100 times, and every count of 20 print my last name. Part of "1000 programming exercises by Chess"
public class InterweavingNames {
public static void main(String[] args) {
String firstname = "Chester";
String lastname = "Grant";
for(int i =1; i<=100; i++){
System.out.print(firstname);
if((i % 20) == 0){
System.out.print(" "+lastname);
@chestergrant
chestergrant / ReadTwoNumbers.java
Created July 1, 2012 07:55
Reads two integers and display them. This is part of "1000 programming exercise by Chess"
import java.util.*;
public class ReadTwoNumbers {
//When reading in digit you need to account for invalid inputs
public static int readNum(String promptMsg, boolean hasPrompt){
boolean isDig = true; //Checks if the value just read in is an integer
int output = -1; //Stores the return value of the read in number
do{
isDig = true;
Scanner input= new Scanner (System.in); //Use to read in the data
@chestergrant
chestergrant / NameTimesHundred.java
Created July 1, 2012 07:25
Prints my name 100 times. Part of "1000 programming exercises by Chess"
public class NameTimesHundred {
public static void main(String[] args) {
for(int i=0; i<100; i++){
System.out.println("Chester");
}
}
}
@chestergrant
chestergrant / TenCount.java
Created July 1, 2012 07:13
Counts from one to ten. Part of "1000 programming exercise by Chess"
public class TenCount {
public static void main(String[] args) {
for(int i=1; i<=10; i++){
System.out.println(i);
}
}
}
@chestergrant
chestergrant / Quicksort_Haskell
Created April 10, 2011 11:08
QuickSort - Haskell
--Got code from: http://www.haskell.org/haskellwiki/Why_Haskell_matters
qsort [] = []
qsort (x:xs) = qsort less ++ [x] ++ qsort more
where less = filter (<x) xs
more = filter (>=x) xs
@chestergrant
chestergrant / quicksort_cpp
Created April 10, 2011 11:06
QuickSort C++
//Got code from: http://www.haskell.org/haskellwiki/Why_Haskell_matters
template <typename T>
void qsort (T *result, T *list, int n)
{
if (n == 0) return;
T *smallerList, *largerList;
smallerList = new T[n];
largerList = new T[n];
T pivot = list[0];
int numSmaller=0, numLarger=0;
@chestergrant
chestergrant / descramble_haskell
Created April 10, 2011 11:04
Word Descrambler - Haskell
-- "Created by: Chester Grant |Got code from:http://www.haskell.org/pipermail/haskell-cafe/2002-June/003122.html";
-- "Purpose: Unscrambles letters(see line 199)\n";
-- "Date: 9th/04/2011\n";
selections [] = []
selections (x:xs) = (x,xs) : [ (y,x:ys) | (y,ys) <- selections xs ]
permutations :: [a] -> [[a]]
permutations [] = [[]]
permutations xs = [ y : zs | (y,ys) <- selections xs, zs <- permutations ys]
@chestergrant
chestergrant / descramble_php
Created April 10, 2011 11:00
Word Descrambler - PHP
<?
$arr = keypadsArr("h,e,s,s,c");
echo "Created by: Chester Grant \n";
echo "Purpose: Unscrambles letters(see line 199)\n";
echo "Date: 9th/04/2011\n";
combination("",0,$arr);
function combination($output,$pos,$dataSet){
if($pos == count($dataSet)){
echo $output."\n";