+ 1

How to solve Hackerrank problem?

So I am solving hackerrank algorithm question but the existing code in the editor seems very complex to me. But I can solve that in a different way, also I satisfied all the test cases (so I basically erase the previous code and write my own). Is this the way to solve these question?

24th Sep 2019, 8:04 AM
Aditya Kumar
Aditya Kumar - avatar
12 Respostas
+ 1
I see, well, the primary task here seems to be to complete the `simpleArraySum` function to get it working as it should. I suggest you follow the instruction. Because even though your code works and does what was needed (sum inputted numbers) your code doesn't employ the `simpleArraySum` and this, go against the instructions. Challenges like this usually be strict with requirements. Failing to follow the instructions (even with goal achieved) may not be accepted unfortunately. Your code directly sum the inputted numbers, not accepting the list of numbers as string (which then be converted and saved into array of int). Since the main instruction was to complete the function I suggest you to do that. There's no instruction to create your own implementation. But I appreciate your active initiation šŸ‘.
27th Sep 2019, 5:14 PM
Ipang
+ 5
Well since you didn't tell which Hackerrank problem it is, and you didn't show the "previous code" or your own. It would be hard to answer the question. However, I want to congratulate you for finding your own way into solving problems. It means you are developing problem solving skills (if your solution satisfies the test cases as you said). Good job! and keep it up šŸ‘
24th Sep 2019, 10:25 AM
Ipang
+ 3
A Huge Thank you!
27th Sep 2019, 5:59 PM
Aditya Kumar
Aditya Kumar - avatar
+ 2
I'll have to get back to this later, will need to move it into a code first to test out.
26th Sep 2019, 11:14 AM
Ipang
+ 2
Thank you very much for answering. Can you please look at this problem description https://www.hackerrank.com/challenges/simple-array-sum/problem
26th Sep 2019, 5:18 PM
Aditya Kumar
Aditya Kumar - avatar
+ 2
You're welcome! and good luck with the challenge šŸ‘
27th Sep 2019, 7:31 PM
Ipang
+ 1
In Practice/Algorithms/warmup/simple Array Sum #previous code #include <bits/stdc++.h> using namespace std; vector<string> split_string(string); /* * Complete the simpleArraySum function below. */ int simpleArraySum(vector<int> ar) { /* * Write your code here. */ } int main() { ofstream fout(getenv("OUTPUT_PATH")); int ar_count; cin >> ar_count; cin.ignore(numeric_limits<streamsize>::max(), '\n'); string ar_temp_temp; getline(cin, ar_temp_temp); vector<string> ar_temp = split_string(ar_temp_temp); vector<int> ar(ar_count); for (int ar_itr = 0; ar_itr < ar_count; ar_itr++) { int ar_item = stoi(ar_temp[ar_itr]); ar[ar_itr] = ar_item; } int result = simpleArraySum(ar); fout << result << "\n"; fout.close(); return 0; } vector<string> split_string(string input_string) { string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { return x == y and x == ' '; }); input_string.erase(new_end, input_string.end()); while (input_string[input_string.length() - 1] == ' ') { input_string.pop_back(); } vector<string> splits; char delimiter = ' '; size_t i = 0; size_t pos = input_string.find(delimiter); while (pos != string::npos) { splits.push_back(input_string.substr(i, pos - i)); i = pos + 1; pos = input_string.find(delimiter, i); } splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); return splits; } #My code #include <bits/stdc++.h> using namespace std; int main(){ vector<int>arr; int n,temp,sum=0; cin>>n; for(int i=0;i<n;i++){ cin>>temp; arr.push_back(temp); sum=sum+arr[i]; } cout<<sum; return 0; } and sorry for late reply
26th Sep 2019, 10:49 AM
Aditya Kumar
Aditya Kumar - avatar
+ 1
ok
26th Sep 2019, 12:40 PM
Aditya Kumar
Aditya Kumar - avatar
+ 1
Okay, I just took a closer look on the code. But unfortunately now I'm not so sure if the original code workflow match your version. Here are a few points I noticed in original code: - Reads an int, presumably as array size. - Reads a string, appears to be a list of numbers, delimited by space. - Splits the string into vector of string, containing numbers (in string format) - Converts each string in vector of string into an int, and save them on a vector of int. - Sums the numbers in the vector of int. Here are a few things that I think is missing in your code: - Original code: Writes the sum of int into a file. - Original code: Numbers were extracted from the string input. Which is splitted into vector of string, which again, converted into int. If any of these differences were one of the requirements then the code may not qualify. I don't mean to be rude buddy, but challenges usually are pretty strict with requirements. Miss one of them - our attempt will not succeed : )
26th Sep 2019, 3:20 PM
Ipang
+ 1
Bro I can't open that link. I don't know why. it says "Couldn't establish a secure connection." Can you post here the challenge requirements/restrictions?
27th Sep 2019, 5:46 AM
Ipang
+ 1
Problem: 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. #Sample Input 6 1 2 3 4 10 11 #Sample Output 31 #Explanation We print the sum of the array's elements: 1+2+3+4+10+11=31
27th Sep 2019, 4:35 PM
Aditya Kumar
Aditya Kumar - avatar
+ 1
Also, try this https://ibb.co/FqLN1mk
27th Sep 2019, 4:37 PM
Aditya Kumar
Aditya Kumar - avatar