diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-09 19:48:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-09 19:48:52 +0100 |
| commit | 7e31b242f4012d100b2949c29f00a3597a9bb80f (patch) | |
| tree | 0255d93790c1e9d28b39a8f13490f1bc6861f543 | |
| parent | cbf4656a599e351b781467b19045a50075027d34 (diff) | |
| parent | ddec8020622ede2a393614fe695c948fb0bd27af (diff) | |
| download | perlweeklychallenge-club-7e31b242f4012d100b2949c29f00a3597a9bb80f.tar.gz perlweeklychallenge-club-7e31b242f4012d100b2949c29f00a3597a9bb80f.tar.bz2 perlweeklychallenge-club-7e31b242f4012d100b2949c29f00a3597a9bb80f.zip | |
Merge pull request #4992 from ealvar3z/branch-for-challenge-132
go solutions with wiki & abigail's blog post
| -rw-r--r-- | challenge-132/ealvar3z/README.md | 1 | ||||
| -rw-r--r-- | challenge-132/ealvar3z/go/ch-1.go | 46 | ||||
| -rw-r--r-- | challenge-132/ealvar3z/go/ch-2.go | 90 |
3 files changed, 137 insertions, 0 deletions
diff --git a/challenge-132/ealvar3z/README.md b/challenge-132/ealvar3z/README.md new file mode 100644 index 0000000000..53d2136f39 --- /dev/null +++ b/challenge-132/ealvar3z/README.md @@ -0,0 +1 @@ +Solutions by ealvar3z diff --git a/challenge-132/ealvar3z/go/ch-1.go b/challenge-132/ealvar3z/go/ch-1.go new file mode 100644 index 0000000000..5d7f04fc3d --- /dev/null +++ b/challenge-132/ealvar3z/go/ch-1.go @@ -0,0 +1,46 @@ +package main + +// functions come from [wiki](https://en.wikipedia.org/wiki/Julian_day#Julian_day_number_calculation) +// translated from [abigail's Perl blog post](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-132-1.html) + +import ( + "fmt" + "log" +) + +// Gregorian to Julian calendar converter +func g2j(Y, M, D int64) int64 { + return ((1461*(Y+4800+(M-14)/12))/4 + + (367*(M-2-12*((M-14)/12)))/12 - + (3*((Y+4900+(M-14)/12)/100))/4 + D - 32075) +} + +// Julian to Gregorian calendar converter +func j2g(jd int64) []int64 { + e := 4*(jd+1401+(((4*jd+274277)/146097)*3)/4-38) + 3 + D := ((5*((e%1461)/4)+2)%153)/5 + 1 + M := (((5*((e%1461)/4)+2)/153 + 2) % 12) + 1 + Y := (e / 1461) - 4716 + (12+2-M)/12 + return []int64{Y, M, D} +} + +var today = []uint{2021, 9, 22} // using this date for testing + +func main() { + //read in the input date + var Y, M, D int64 + fmt.Println("input yr, mon, and date on each line") // Question to the user + if _, err := fmt.Scan(&Y, &M, &D); err != nil { + log.Println(err) + } + + today_y := int64(today[0]) + today_m := int64(today[1]) + today_d := int64(today[2]) + + julian_2day := g2j(today_y, today_m, today_d) + julian_then := g2j(Y, M, D) + then := j2g(2*julian_then - julian_2day) + now := j2g(2*julian_2day - julian_then) + fmt.Printf("%04d/%02d/%02d => %04d/%02d/%02d\n", then[0], then[1], then[2], now[0], now[1], now[2]) +} diff --git a/challenge-132/ealvar3z/go/ch-2.go b/challenge-132/ealvar3z/go/ch-2.go new file mode 100644 index 0000000000..b0b1f6f5fb --- /dev/null +++ b/challenge-132/ealvar3z/go/ch-2.go @@ -0,0 +1,90 @@ +package main + +/* +Write a script to implement Hash Join algorithm as suggested by wikipedia. + +The "hash join" algorithm consists of two steps: + +1. Hash phase: + 1.1 - Create a multimap from one of the two tables, mapping from each join column value to all the rows that contain it. The multimap must support hash-based lookup which scales better than a simple linear s earch, because that's the whole point of this algorithm. Ideally we should create the multimap for the smaller table, thus minimizing its creation time and memory size. + +2. Join phase: + 2.1 - Scan the other table, and find matching rows by looking in the multimap created before. + +Test Case + +Input: + + @player_ages = ( + [20, "Alex" ], + [28, "Joe" ], + [38, "Mike" ], + [18, "Alex" ], + [25, "David" ], + [18, "Simon" ], + ); + + @player_names = ( + ["Alex", "Stewart"], + ["Joe", "Root" ], + ["Mike", "Gatting"], + ["Joe", "Blog" ], + ["Alex", "Jones" ], + ["Simon","Duane" ], + ); + +Output: + + Based on index = 1 of @players_age and index = 0 of @players_name. + + 20, "Alex", "Stewart" + 20, "Alex", "Jones" + 18, "Alex", "Stewart" + 18, "Alex", "Jones" + 28, "Joe", "Root" + 28, "Joe", "Blog" + 38, "Mike", "Gatting" + 18, "Simon", "Duane" + +*/ + +import "fmt" + +func main() { + playerAges := []struct { + value int + key string + }{ + {20, "Alex"}, + {28, "Joe"}, + {38, "Mike"}, + {18, "Alex"}, + {25, "David"}, + {18, "Simon"}, + } + + playerNames := []struct { + key string + value string + }{ + {"Alex", "Stewart"}, + {"Joe", "Root"}, + {"Mike", "Gatting"}, + {"Joe", "Blog"}, + {"Alex", "Jones"}, + {"Simon", "Duane"}, + } + + // build the hash phase + h := map[string][]int{} + for n, a := range playerAges { + h[a.key] = append(h[a.key], n) + } + + // join phase + for _, i := range playerNames { + for _, j := range h[i.key] { + fmt.Println("Based on index = 1 of", playerAges[j], "and index = 0 of", i.value) + } + } +} |
