Last active
January 29, 2018 22:04
-
-
Save mohanraj-r/109c2c8900b79108c4a7a1ddb1a1cd2d to your computer and use it in GitHub Desktop.
[scratch.go] #go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To execute Go code, please declare a func main() in a package "main" | |
// given an arbitrary input, find the substring that is closest to, but not greater than 1.00 where a=0.01 and z=0.26 | |
// exit on finding 1.00 | |
// if over or under keep scanning till end of string | |
// ascii only input - check and sanitize | |
// ignore uppercase - only process a-z | |
// g = 20 | |
// i = 25 | |
// v = 30 | |
// e = 40 | |
// n = 50 | |
// g 20 | |
// gi 20+25 | |
// giv 20+25+30 | |
// give 20+25+30+40 X | |
// ive <> | |
// iven | |
// ven | |
package main | |
import "fmt" | |
func getWeight(c rune) (weight float32) { | |
index := 1 + int(c) - int('a') | |
return float32(index) * float32(0.01) | |
} | |
func convertWeights(s string) []float32 { | |
var weights []float32 | |
for _, c := range s { | |
weights = append(weights, getWeight(c)) | |
} | |
return weights | |
} | |
func Scannner(s string) (substring string) { | |
weights := convertWeights(s) | |
maxWeight := float32(0.0) | |
l := len(weights) | |
startIndex, endIndex := 0, 0 | |
fmt.Printf("\nWeights for '%s': %v\n", s, weights) | |
for i:=0; i<l-1; i++ { | |
weight := weights[i] | |
startIndex = i | |
for j:=i+1; j<l; j++ { | |
endIndex = j | |
weight += weights[j] | |
fmt.Println(i, j, weight) | |
if weight > 1.00 { | |
// rewind back one position | |
weight -= weights[j] | |
endIndex = j-1 | |
break | |
} | |
} | |
fmt.Printf("Weight of index %d-%d : %f\n\n", startIndex, endIndex, weight) | |
if weight > maxWeight { | |
substring = s[startIndex:endIndex+1] | |
fmt.Printf("Replacing %f with %f as maxweight. Substring: '%s'\n", | |
maxWeight, weight, substring) | |
maxWeight = weight | |
} | |
} | |
fmt.Printf("For string '%s' substring '%s' has max weight of %f\n\n", | |
s, substring, maxWeight) | |
return substring | |
} | |
func main() { | |
Scannner("given") | |
Scannner("zabzaazaazaz") | |
// Output: | |
// For string 'given' substring 'given' has max weight of 0.570000 | |
// For string 'zabzaazaazaz' substring 'abzaazaaza' has max weight of 0.860000 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To execute Go code, please declare a func main() in a package "main" | |
package main | |
import "fmt" | |
import "sync" | |
/* | |
TODO: | |
* Concurrency | |
* DS specific to api patterns | |
*/ | |
const ( | |
lowerBound = 1 | |
upperBound = 10000000000 | |
) | |
// TODO: lock with mutex | |
var reserved = make(map[int64]bool, upperBound-lowerBound) | |
var mu = &sync.Mutex{} | |
func boundsCheck() bool { | |
if len(reserved) == upperBound-lowerBound { | |
return false | |
} | |
return true | |
} | |
func reserve(num int64) { | |
mu.Lock() | |
defer mu.Unlock() | |
reserved[num] = true | |
} | |
func unreserve(num int64) { | |
// TODO: is deleting better than marking it as false, in terms of allocations etc ? | |
mu.Lock() | |
defer mu.Unlock() | |
reserved[num] = false | |
// delete(reserved, num) | |
} | |
// Reserve the next available integer between 1 and 10,000,000,000. // Return the integer, or 0 if none are available. | |
func NextAvailable() (next int64) { | |
if b := boundsCheck(); b == false { | |
return 0 | |
} | |
for i := lowerBound; i < upperBound; i++ { | |
i := int64(i) | |
exists := reserved[i] | |
if !exists { | |
reserve(i) | |
next = i | |
break | |
} | |
} | |
return next | |
} | |
// Reserve a specific integer between 1 and 10,000,000,000. | |
// Reserve and return true if it is available, false if it is not. | |
func Reserve(num int64) bool { | |
if b := boundsCheck(); b == false { | |
return false | |
} | |
exists := reserved[num] | |
if !exists { | |
reserve(num) | |
return true | |
} | |
return false | |
} | |
// Un-reserve (i.e. "free up") a specific integer between 1 and 10,000,000,000. Return false if it were not previously reserved, otherwise true. | |
func UnReserve(num int64) bool { | |
v, ok := reserved[num] | |
if ok && v { | |
unreserve(num) | |
return true | |
} | |
return false | |
} | |
func main() { | |
// TODO: turn these into tests | |
fmt.Println("Reserve") | |
fmt.Println(Reserve(1)) | |
fmt.Println(Reserve(1)) | |
fmt.Println(Reserve(2)) | |
fmt.Println(Reserve(3)) | |
fmt.Println(Reserve(2)) | |
fmt.Println("NextAvailable") | |
fmt.Println(NextAvailable()) | |
fmt.Println(Reserve(NextAvailable())) | |
fmt.Println(NextAvailable()) | |
fmt.Println(NextAvailable()) | |
fmt.Println("UnReserve") | |
fmt.Println(UnReserve(2)) | |
fmt.Println(UnReserve(2)) | |
fmt.Println(UnReserve(2)) | |
fmt.Println(UnReserve(1)) | |
fmt.Println(UnReserve(1)) | |
fmt.Println("NextAvailable") | |
fmt.Println(NextAvailable()) | |
fmt.Println(NextAvailable()) | |
fmt.Println(NextAvailable()) | |
/* Output: | |
Reserve | |
true | |
false | |
true | |
true | |
false | |
NextAvailable | |
4 | |
false | |
6 | |
7 | |
UnReserve | |
true | |
false | |
false | |
true | |
false | |
NextAvailable | |
1 | |
2 | |
8 | |
*/ | |
} | |
/* | |
Your previous Plain Text content is preserved below: | |
Write a program that supports the following operations. | |
- Reserve the next available integer between 1 and 10,000,000,000. Return the integer, or 0 if none are available. | |
- Reserve a specific integer between 1 and 10,000,000,000. Reserve and return true if it is available, false if it is not. | |
- Un-reserve (i.e. "free up") a specific integer between 1 and 10,000,000,000. Return false if it were not previously reserved, otherwise true. | |
Dealing with error conditions is left as an exercise to the coder. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Duck typing using interface | |
package main | |
import "fmt" | |
type Quacker interface { | |
Quack() | |
} | |
type Duck struct { | |
} | |
func (d Duck) Quack() { | |
fmt.Println("Duck says Quack!") | |
} | |
type Platypus struct { | |
} | |
func (p Platypus) Quack() { | |
fmt.Println("Platypus zaps izzz..") | |
} | |
type Bird struct { | |
q Quacker | |
} | |
func main() { | |
d := Duck{} | |
b := Bird{Quacker(d)} | |
b.q.Quack() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment