We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Tutorials
- 30 Days of Code
- Day 7: Arrays
- Discussions
Day 7: Arrays
Day 7: Arrays
+ 0 comments Java 15
import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.function.; import java.util.regex.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList;
public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bufferedReader.readLine().trim()); List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) .map(Integer::parseInt) .collect(toList()); bufferedReader.close(); for (int i=arr.size()-1; i >= 0; i--){ System.out.print(arr.get(i) + " ");
} } }
+ 0 comments javascript solution, only 2 line of code. const I change to let to make the variable mutable
function main() { const n = parseInt(readLine().trim(), 10); let arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10)); arr = arr.reverse(); console.log(arr.join(' ')); }
+ 0 comments C++ solution I hope this gives you an idea and learn how this one works
#include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); vector<string> split(const string &); int main() { string n_temp; getline(cin, n_temp); int n = stoi(ltrim(rtrim(n_temp))); string arr_temp_temp; getline(cin, arr_temp_temp); vector<string> arr_temp = split(rtrim(arr_temp_temp)); vector<int> arr(n); for (int i = 0; i < n; i++) { //n = size of array int arr_item = stoi(arr_temp[i]); //convert string to int(?) arr_temp[i] = arr[n - i - 1]; //swap element arr[n - i - 1] = arr_item; //swap element } for(int i = 0; i < n; i++){ cout << arr[i] << " "; //prints the reversed } return 0; } string ltrim(const string &str) { string s(str); s.erase( s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))) ); return s; } string rtrim(const string &str) { string s(str); s.erase( find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), s.end() ); return s; } vector<string> split(const string &str) { vector<string> tokens; string::size_type start = 0; string::size_type end = 0; while ((end = str.find(" ", start)) != string::npos) { tokens.push_back(str.substr(start, end - start)); start = end + 1; } tokens.push_back(str.substr(start)); return tokens; }
+ 0 comments n, arr = input(), input().split() print(*arr[::-1])
+ 1 comment Python 3
Can someone please tell me what's wrong here ?
a = n-1
while (a<-1): print (arr[a], sep="") a -= 1
Load more conversations
Sort 1916 Discussions, By:
Please Login in order to post a comment