• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

How to pass all 3 test cases?  RSS feed

 
Ranch Hand
Posts: 491
  • Mark post as helpful
  • send pies
  • 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: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 64086
215
  • Mark post as helpful
  • send pies
  • 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: 491
  • Mark post as helpful
  • send pies
  • 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: 64086
215
  • Mark post as helpful
  • send pies
  • 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.
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!