Last active
December 20, 2020 23:00
-
-
Save bbuck/8cb4a69e6e9b9deb1fa9 to your computer and use it in GitHub Desktop.
Solutions to a hacker rank problem demonstrating slowness in 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
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"strconv" | |
) | |
func main() { | |
scanner := bufio.NewScanner(os.Stdin) | |
scanner.Split(bufio.ScanWords) | |
scanner.Scan() | |
t, _ := strconv.Atoi(scanner.Text()) | |
for x := 0; x < t; x++ { | |
scanner.Scan() | |
n, _ := strconv.Atoi(scanner.Text()) | |
total := 0 | |
values := make([]int, n) | |
for i := 0; i < n; i++ { | |
scanner.Scan() | |
value, _ := strconv.Atoi(scanner.Text()) | |
total += value | |
values[i] = value | |
} | |
left := total | |
right := 0 | |
result := "NO" | |
for i := 0; i < n; i++ { | |
left -= values[i] | |
if right == left { | |
result = "YES" | |
break | |
} | |
right += values[i] | |
} | |
fmt.Println(result) | |
} | |
} |
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
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
) | |
func main() { | |
reader := bufio.NewReader(os.Stdin) | |
var t int | |
fmt.Fscanf(reader, "%v\n", &t) | |
for x := 0; x < t; x++ { | |
var n, total, value int | |
fmt.Fscanf(reader, "%v", &n) | |
values := make([]int, n) | |
for i := 0; i < n; i++ { | |
fmt.Fscanf(reader, "%v", &value) | |
total += value | |
values[i] = value | |
} | |
left := total | |
right := 0 | |
result := "NO" | |
for i := 0; i < n; i++ { | |
left -= values[i] | |
if right == left { | |
result = "YES" | |
break | |
} | |
right += values[i] | |
} | |
fmt.Println(result) | |
} | |
} |
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
#include <stdio.h> | |
#include <stdlib.h> | |
int main(void) { | |
long t = 0; | |
long n = 0; | |
long *values; | |
scanf("%ld", &t); | |
for (long x = 0; x < t; x++) { | |
scanf("%ld", &n); | |
long total = 0; | |
long value = 0; | |
values = malloc(sizeof(long) * n); | |
for (long i = 0; i < n; i++) { | |
scanf("%ld", &value); | |
total += value; | |
values[i] = value; | |
} | |
long left = total; | |
long right = 0; | |
int yes = 0; | |
for (long i = 0; i < n; i++) { | |
left -= values[i]; | |
if (right == left) { | |
yes = 1; | |
break; | |
} | |
right += values[i]; | |
} | |
printf("%s\n", (yes ? "YES" : "NO")); | |
free(values); | |
} | |
} |
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
package main | |
import "fmt" | |
func main() { | |
var t int | |
fmt.Scanf("%v", &t) | |
for x := 0; x < t; x++ { | |
var n, total, value int | |
fmt.Scanf("%v", &n) | |
values := make([]int, n) | |
for i := 0; i < n; i++ { | |
fmt.Scanf("%v", &value) | |
total += value | |
values[i] = value | |
} | |
left := total | |
right := 0 | |
result := "NO" | |
for i := 0; i < n; i++ { | |
left -= values[i] | |
if right == left { | |
result = "YES" | |
break | |
} | |
right += values[i] | |
} | |
fmt.Println(result) | |
} | |
} |
@robpike, Thanks for input - I definitely should have done - totally a failure on me. I haven't used Scan or Scanf a lot (actually never until I started doing these challenges) so I'm relatively new to their offerings. I'll comb over it again doing my due diligence.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check your errors! It's likely you're getting errors from the scan calls and by ignoring them you are not finding the problems in your code.
I would also like to point out that when using Scanf or Fscanf, newlines are significant; moreover, the two versions have different newline placements.
Also, since you're only using %v there is no need to use Scanf; Scan would do find and will avoid the newlines issue.
All of these details are part of why the Scan interface is a throwback that should be avoided except for the simplest uses.