-
Notifications
You must be signed in to change notification settings - Fork 84
latihan golang basic #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
latihan golang basic #104
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| var kata string | ||
|
|
||
| fmt.Scan(&kata) | ||
|
|
||
| result := isPalindrome(kata) | ||
|
|
||
| fmt.Println(result) | ||
| } | ||
|
|
||
| func isPalindrome(kata string) (isPalindrome bool) { | ||
| for i := 0; i < len(kata)/2; i++ { | ||
| if kata[i] == kata[len(kata)-i-1] { | ||
| isPalindrome = true | ||
| } | ||
| } | ||
| return | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package main | ||
|
|
||
| import "testing" | ||
|
|
||
| func Test_isPalindrome(t *testing.T) { | ||
| type args struct { | ||
| kata string | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| args args | ||
| wantIsPalindrome bool | ||
| }{ | ||
| { | ||
| name : "test", | ||
| args : args{ | ||
| "katak", | ||
| }, | ||
| wantIsPalindrome : true, | ||
| }, | ||
| { | ||
| name : "test", | ||
| args : args{ | ||
| "kata", | ||
| }, | ||
| wantIsPalindrome : false, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if gotIsPalindrome := isPalindrome(tt.args.kata); gotIsPalindrome != tt.wantIsPalindrome { | ||
| t.Errorf("isPalindrome() = %v, want %v", gotIsPalindrome, tt.wantIsPalindrome) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkIsPalindrome(b *testing.B) { | ||
| for i := 0; i < b.N; i++ { | ||
| isPalindrome("katak") | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module tugas1 | ||
|
|
||
| go 1.17 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| func main() { | ||
| var kata string | ||
|
|
||
| fmt.Scan(&kata) | ||
|
|
||
| kata2 := kata[0] | ||
|
|
||
| result := checkConsonant(string(kata2)) | ||
|
|
||
| fmt.Print(result) | ||
| } | ||
|
|
||
| func checkConsonant(ch string) string { | ||
| ch = strings.ToLower(ch) | ||
|
|
||
| if ch == "a" || ch == "i" || ch == "u" || ch == "e" || ch == "o" { | ||
| return "Vokal" | ||
| } | ||
|
|
||
| return "Konsonan" | ||
| } | ||
|
|
||
| func checkConsonantWithPackage(input string) string { | ||
| vowels := "aeiou" | ||
|
|
||
| if strings.Contains(vowels, input) { | ||
| return "Vokal" | ||
| } | ||
|
|
||
| return "Konsonan" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package main | ||
|
|
||
| import "testing" | ||
|
|
||
| func Test_checkConsonant(t *testing.T) { | ||
| type args struct { | ||
| ch string | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| args args | ||
| want string | ||
| }{ | ||
| { | ||
| name: "Test untuk konsonan", | ||
| args: args{ | ||
| ch: "k", | ||
| }, | ||
| want: "Konsonan", | ||
| }, | ||
| { | ||
| name: "Test untuk konsonan 2", | ||
| args: args{ | ||
| ch: "p", | ||
| }, | ||
| want: "Konsonan", | ||
| }, | ||
| { | ||
| name: "Test untuk Vokal a", | ||
| args: args{ | ||
| ch: "a", | ||
| }, | ||
| want: "Vokal", | ||
| }, | ||
| { | ||
| name: "Test untuk Vokal i", | ||
| args: args{ | ||
| ch: "i", | ||
| }, | ||
| want: "Vokal", | ||
| }, | ||
| { | ||
| name: "Test untuk Vokal u", | ||
| args: args{ | ||
| ch: "u", | ||
| }, | ||
| want: "Vokal", | ||
| }, | ||
| { | ||
| name: "Test untuk Vokal e", | ||
| args: args{ | ||
| ch: "e", | ||
| }, | ||
| want: "Vokal", | ||
| }, | ||
| { | ||
| name: "Test untuk Vokal o", | ||
| args: args{ | ||
| ch: "o", | ||
| }, | ||
| want: "Vokal", | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := checkConsonant(tt.args.ch); got != tt.want { | ||
| t.Errorf("checkConsonant() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_checkConsonantWithPackage(t *testing.T) { | ||
| type args struct { | ||
| input string | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| args args | ||
| want string | ||
| }{ | ||
| { | ||
| name: "Test untuk konsonan", | ||
| args: args{ | ||
| input: "k", | ||
| }, | ||
| want: "Konsonan", | ||
| }, | ||
| { | ||
| name: "Test untuk konsonan 2", | ||
| args: args{ | ||
| input: "p", | ||
| }, | ||
| want: "Konsonan", | ||
| }, | ||
| { | ||
| name: "Test untuk Vokal a", | ||
| args: args{ | ||
| input: "a", | ||
| }, | ||
| want: "Vokal", | ||
| }, | ||
| { | ||
| name: "Test untuk Vokal i", | ||
| args: args{ | ||
| input: "i", | ||
| }, | ||
| want: "Vokal", | ||
| }, | ||
| { | ||
| name: "Test untuk Vokal u", | ||
| args: args{ | ||
| input: "u", | ||
| }, | ||
| want: "Vokal", | ||
| }, | ||
| { | ||
| name: "Test untuk Vokal e", | ||
| args: args{ | ||
| input: "e", | ||
| }, | ||
| want: "Vokal", | ||
| }, | ||
| { | ||
| name: "Test untuk Vokal o", | ||
| args: args{ | ||
| input: "o", | ||
| }, | ||
| want: "Vokal", | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := checkConsonantWithPackage(tt.args.input); got != tt.want { | ||
| t.Errorf("checkConsonantWithPackage() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkCheckConsonant(b *testing.B) { | ||
| for i := 0; i < b.N; i++ { | ||
| checkConsonant("a") | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkCheckConsonantWithPackage(b *testing.B) { | ||
| for i := 0; i < b.N; i++ { | ||
| checkConsonantWithPackage("a") | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module tugas2 | ||
|
|
||
| go 1.17 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bagus sudah memakai func ini, agar seragam string yang akan dijadikan kondisinya yaa