• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to pass all 3 test cases?

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Today, I learnt about this hacker test and I attempted it.  Feeling very depressed now...I think I am really not cut out to do software job cos even this simple question I cant do.

The most I got correct is the first test.

I hope someone can tell me how to past all 3 tests.


Given an array of integers, find the sum of its elements.

For example, if the array ar = [1,2,3], 1+2+3 = 6, so return 6



Function Description

Complete the simpleArraySum function in the editor below. It must return the sum of the array elements as an integer.

simpleArraySum has the following parameter(s):

   ar: an array of integers

Input Format

The first line contains an integer, n,  denoting the size of the array.
The second line contains n space-separated integers representing the array's elements.

Constraints : 0 < n, ar[i]<=1000

Output Format

Print the sum of the array's elements as a single integer




 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first one you got right?
The second one is a string containing the space seperated values? In that case if you do
but I just see it's an array? so then it would be
you're left with an array of numeric values that you can parse like you have tried.
The tirth is unclear to me way you would use a BufferedWriter instead of System.out.println
Maybe you should post the question as it's asked, because I don't really understand the difference between 1 and 3
you also create a lot of variables you don't need
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 1:

Question 2:
 
Question 3 I'm not sure what you're trying to do, so I'm not touching it
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I actually noticed a couple of mistake in my previous post, in the first question I forget a ')' at my for each.
Ad for question 2, that should be:
 
Marshal
Posts: 79406
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using System.getEnv(...) rather than System.out?
Why didn't you use try with resources to close your buffered writer?
Why are you using String#split in conjunction with a Scanner?
 
tangara goh
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why are you using System.getEnv(...) rather than System.out?
Why didn't you use try with resources to close your buffered writer?
Why are you using String#split in conjunction with a Scanner?



This doesn't come from me.  It is hanker rank's code which I found it very difficult.
 
Campbell Ritchie
Marshal
Posts: 79406
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tangara goh wrote:. . . This doesn't come from me.

Thank goodness for hat.

It is hanker rank's code which I found it very difficult.

That is because it is badly written. It is even better that I tried OUTPUT_PATH on my computer twice and got null! I shall let you find out what happens if you use that nul. This is whatt I would have written:-It should be obvious that I have shamelessly missed bits out.
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic