Skip to content

Instantly share code, notes, and snippets.

@kaipakartik
kaipakartik / fibonacci.go
Created December 25, 2013 01:09
Solution to the fibonacci problem using function closures in go http://tour.golang.org/#44
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
current := 0
a := 0
b := 1
@kaipakartik
kaipakartik / complexpower.go
Created December 25, 2013 01:18
Advanced Exercise: Complex cube roots http://tour.golang.org/#48
package main
import (
"fmt"
"math/cmplx"
)
func Cbrt(x complex128) complex128 {
z := complex128(1)
for i := 0; i < 10; i++ {
@kaipakartik
kaipakartik / loops.go
Last active January 1, 2016 08:39
My solution to the loops and functions exercise http://tour.golang.org/#24
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
next := float64(1)
prev := float64(0)
@kaipakartik
kaipakartik / slices.go
Created December 25, 2013 00:55
Solution to the slices exercise in Go
package main
import "code.google.com/p/go-tour/pic"
func Pic(dx, dy int) [][]uint8 {
y := make([][]uint8, dy)
for i := range y {
y[i] = make([]uint8, dx)
}
y = transform(y)
@kaipakartik
kaipakartik / wordcount.go
Created December 25, 2013 01:01
Solution to the maps exercise on golang http://tour.golang.org/#41
package main
import (
"code.google.com/p/go-tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
words := strings.Fields(s)
wordcount := make(map[string]int)
@kaipakartik
kaipakartik / gist:8119470
Created December 25, 2013 01:42
Exercise: HTTP Handlers Implement the following types and define ServeHTTP methods on them. Register them to handle specific paths in your web server. http://tour.golang.org/#58
package main
import (
"fmt"
"net/http"
)
type String string
type Struct struct {
@kaipakartik
kaipakartik / image.go
Created December 25, 2013 02:04
Exercise: Images Remember the picture generator you wrote earlier? Let's write another one, but this time it will return an implementation of image.Image instead of a slice of data.
package main
import (
"code.google.com/p/go-tour/pic"
"image/color"
"image"
)
type Image struct{
width, height int
@kaipakartik
kaipakartik / rot13.go
Created December 25, 2013 02:21
Exercise: Rot13 Reader A common pattern is an io.Reader that wraps another io.Reader, modifying the stream in some way.
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
@kaipakartik
kaipakartik / problem8_euler.py
Created January 10, 2014 01:20
Find the greatest product of five consecutive digits in the 1000-digit number. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 6222989342338…
def largest_product(number):
maxProduct = 1
for i in range(len(number) - 5):
product = reduce(lambda x,y : int(x) * int (y), number[i: i + 5])
if product > maxProduct:
maxProduct = product
return maxProduct
a = \
'73167176531330624919225119674426574742355349194934' +\
@kaipakartik
kaipakartik / problem7_euler.py
Last active January 2, 2016 18:39
Problem 7 Project Euler By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number?
def prime(n):
primes = [2]
start = 3
while len(primes) <= n:
isPrime = True;
for prime in primes:
if start % prime == 0:
isPrime = False;
break;
if isPrime: