From 4ec57ef9091c1af65cff69651fb806236a6cf71a Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Sun, 17 Jul 2022 22:24:55 +0700 Subject: pwc010 solution in go --- challenge-010/pokgopun/README | 1 + challenge-010/pokgopun/go/ch-1.go | 58 +++++++++++++++++++++++++++++++++++++++ challenge-010/pokgopun/go/ch-2.go | 24 ++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 challenge-010/pokgopun/README create mode 100644 challenge-010/pokgopun/go/ch-1.go create mode 100644 challenge-010/pokgopun/go/ch-2.go diff --git a/challenge-010/pokgopun/README b/challenge-010/pokgopun/README new file mode 100644 index 0000000000..33dfd303a4 --- /dev/null +++ b/challenge-010/pokgopun/README @@ -0,0 +1 @@ +Solution by PokGoPun diff --git a/challenge-010/pokgopun/go/ch-1.go b/challenge-010/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..5101221a9d --- /dev/null +++ b/challenge-010/pokgopun/go/ch-1.go @@ -0,0 +1,58 @@ +/* https://theweeklychallenge.org/blog/perl-weekly-challenge-010/ + +Challenge #1 +Write a script to encode/decode Roman numerals. For example, given Roman numeral CCXLVI, it should return 246. Similarly, for decimal number 39, it should return XXXIX. Checkout wikipedia page for more informaiton. + +*/ +package main + +import ( + "fmt" + "log" + "os" + "strconv" + + "github.com/pokgopun/go/roman" +) + +func main() { + guide := ` +Usage: + To encode decimal number to roman, enter: + %cmd% encode %number% + + To decode roman number string to decimal number, enter: + %cmd% decode %string% + ` + roman := roman.NewRoman() + if len(os.Args) > 2 && (os.Args[1] == "encode" || os.Args[1] == "decode") { + switch os.Args[1] { + case "encode": + n, err := strconv.ParseUint(os.Args[2], 10, 64) + if err != nil { + log.Fatal(err) + } + str, err := roman.FromDec(uint(n)) + if err != nil { + log.Fatal(err) + } + fmt.Println(str) + case "decode": + n, err := roman.ToDec(os.Args[2]) + if err != nil { + log.Fatal(err) + } + fmt.Println(n) + } + } else { + fmt.Println(guide) + for _, v := range []uint{246, 39} { + str, _ := roman.FromDec(v) + fmt.Printf("\tencode %v => %v\n\n", v, str) + } + for _, v := range []string{"CCXLVI", "XXXIX"} { + n, _ := roman.ToDec(v) + fmt.Printf("\tdecode %v => %v\n\n", v, n) + } + } +} diff --git a/challenge-010/pokgopun/go/ch-2.go b/challenge-010/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..838269806e --- /dev/null +++ b/challenge-010/pokgopun/go/ch-2.go @@ -0,0 +1,24 @@ +/* https://theweeklychallenge.org/blog/perl-weekly-challenge-010/ +Challenge #2 +Write a script to find Jaro-Winkler distance between two strings. For more information check wikipedia page. +*/ +package main + +import ( + "fmt" + "os" + + "github.com/pokgopun/go/jarowinkler" +) + +func main() { + if len(os.Args) > 2 { + jw := jarowinkler.New(os.Args[1], os.Args[2]) + //fmt.Printf("%.3f %.3f\n", jw.Similarity(false), jw.Similarity(true)) + fmt.Printf("%.3f\n", jw.Distance(true)) + } else { + fmt.Println(` +Usage: + %cmd% %str1% %str2%`) + } +} -- cgit