+ 1

Need C++ code equivalent for the following Python solution to the code coach "Divisible"

I first tried solving the coach using C++ but couldn't get it out, but managed to solve it fairly easily in python 🐍. Would anyone be able to essentially translate my python code into C++ working code that provides the same solution, so I can compare and see where I'm going wrong? I'm really trying to improve my C++ skills 👾👾 https://code.sololearn.com/cbhV03c4dz01/?ref=app

14th Apr 2023, 10:02 PM
Matt Osmond
Matt Osmond - avatar
28 Antworten
+ 6
A very simple idea: when the sum of all the remainders is zero, then the number is divisible by all. 6 lines of c++, no fancy libraries. int n, div, sum=0; cin >> n; while (cin >> div) sum += n % div; if (sum > 0) cout << "not "; cout << "divisible by all"; https://code.sololearn.com/cm08Di019aHH/?ref=app
15th Apr 2023, 8:57 AM
Tibor Santa
Tibor Santa - avatar
+ 10
Matt Osmond , > sololearn is *self-learner* platform. so the priority is learning coding. > the asker can get help, as long as he has done a a serious try and effort to solve the problem. if we give ready-made codes, the learning effect is very small. > this principle of learning and helping is also communicated from sololearn and the community mentors.
15th Apr 2023, 11:14 AM
Lothar
Lothar - avatar
+ 7
i am a little bit confused! > why is so much help offered to create a c++ code for a *code coach exercise* when the asker hasn't shown any attempt in c++ that he tried by himself ?
15th Apr 2023, 10:53 AM
Lothar
Lothar - avatar
+ 3
Matt Osmond It's always a good idea to learn the nuances of the language. While different programming languages can be written in the same pattern, they often have unique features and constructs that are specific to them and learning to use them is always worthwhile. Write Python the Python way, C++ the C++ way, etc. Direct translations might work, but they are rarely optimal.
15th Apr 2023, 3:43 AM
Bob_Li
Bob_Li - avatar
+ 3
Bob_Li yes something like that. This was my Python version: n = int(input()) d = map(int, input().split()) s = sum(n % x for x in d) print(["not ", ""][s==0] + "divisible by all") Again the printing is a bit unconventional, but learning array languages like BQN leaves a mark on my coding style :)
15th Apr 2023, 10:34 AM
Tibor Santa
Tibor Santa - avatar
+ 3
Lothar it's not like there was no effort at all. Matt posted his Python code. And I think it's been a very interesting and productive exchange of ideas for everyone, including me, we've all learned from each other and stayed on topic. There are many ways to solve the same problem. There can even be many "best" ways, and how we approach a problem often comes down to personal experience, knowledge of this or that language element, and even preferred coding style. "Comparing notes" is fun and beneficial for all.
15th Apr 2023, 2:51 PM
Tibor Santa
Tibor Santa - avatar
+ 3
Sure, here is the code in advanced C++ syntax. #include <iostream> #include <vector> #include <string> using namespace std; int main() { int numerator; string denominators; vector<int> denomList; int count = 0; // get input cout << "Enter the numerator: "; cin >> numerator; cout << "Enter the denominators (separated by spaces): "; cin.ignore(); getline(cin, denominators); // convert denominators to vector of ints stringstream ss(denominators); int temp; while (ss >> temp) denomList.push_back(temp); // check if numerator is divisible by all denominators for (int x : denomList) { if (numerator % x == 0) continue; // numerator is divisible, check next denominator else { count++; // numerator is not divisible, increment count break; // no need to check remaining denominators } } // output result if (count > 0) cout << "not divisible by all" << endl; els
16th Apr 2023, 1:03 AM
VIBED
+ 2
Matt Osmond The code coach does not require any output if there is an error, so I did not include it. It would probably not be used in the checking anyway. (The right thing to do is to include it. I was just being lazy..😅) But if you are willing to use less commonly used methods, here is the c++ solution I used. more about find_if_not https://cplusplus.com/reference/algorithm/find_if_not/ lambda expression https://learn.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp https://code.sololearn.com/c9h7IK3oc20A/?ref=app
15th Apr 2023, 2:45 AM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li thanks for both of these -- your C++ methods are a little out of my scope right now but definitely saving for future reference/upskilling 👾 And the Python solution is shorter and more elegant than mine as well -- love it! 🐍
15th Apr 2023, 3:33 AM
Matt Osmond
Matt Osmond - avatar
+ 2
Bob_Li 100% agree 💯 Having a university understanding of calculus and linear algebra, I find the logic of Python to naturally align with my own academic logic. C++ however, can do things in ways that python can't and vice versa. I only used the direct translation approach this time because I ended up getting hogtied by my own C++ syntax and needed to structure it in a way that made sense in my head 👾 The more I practice C++, the more I will intuitively use its logic. Python just comes more naturally to me 🐍 You have been a great help! 🙌🙌🙌
15th Apr 2023, 3:48 AM
Matt Osmond
Matt Osmond - avatar
+ 2
Tibor Santa 🤯🤯🤯mind blown🤯🤯🤯 processing while taking the input. 😎😎😎brilliant😎😎😎 multitasking achievement unlocked! even your cout is elegantly crafted. simply beautiful. i am saving your code...🤩🤩 This time I get back more than I give away. That's a win everytime.
15th Apr 2023, 9:33 AM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li thank you for the praise :)
15th Apr 2023, 9:58 AM
Tibor Santa
Tibor Santa - avatar
+ 2
All my fault. I often tend to get overly enthusiastic.😅😅😅Sidetracking questions seems to be my weakness. Redoing the courses just gets so mind-numbing. And no interesting question seems to to be raised lately. Seeing Mirielle 's code just triggered the 'let's optimize this thing' mode in me. That snowballed quickly, though...😁 Everything becomes a brainstorming session for me. If someone reading this thread gets motivated to write concise, elegant codes, then I think all this effort is not wasted. Tibor Santa BQN... yes maybe I should move it up my todo list.. Matt Osmond There are lots of good ideas in here. Hope they help you to level-up your coding game😁. Big bro Lothar here thinks you should put more effort into it... 😅
15th Apr 2023, 11:26 AM
Bob_Li
Bob_Li - avatar
+ 2
Note: I have used a vector instead of an array to store the denominators and a stringstream to convert the input string to a vector of ints. This code also handles input errors by ignoring any leading whitespaces and using getline to get the entire input line, instead of just the first integer.
16th Apr 2023, 1:10 AM
VIBED
+ 2
VIBED This has helped heaps! Particularly your explainer as to why you used vectors and stringstream -- I suck at that part of C++ and need to upskill. Also, thanks for reminding me that you can do for loops in C++ using the for(int x: denomList) {} syntax -- I had been resorting to using for (int i = 0; i < some_value; i++) {} to iterate over arrays and it cuts out so many unnecessary lines to write it in the shortened version 👾 Another super useful contribution to my learning; thanks! 🙏🙏
16th Apr 2023, 1:33 AM
Matt Osmond
Matt Osmond - avatar
+ 2
Always happy to help 😎 also that's the great thing about C++, once you learn something it can be remembered so much easier than other languages (my personal opinion). Something about it is also incredibly beautiful to look at 😅 almost like art in a way.
16th Apr 2023, 1:44 AM
VIBED
+ 2
oh, there's definitely an art to coding anything in an optimised way I feel. You can have all the theoretical knowledge and practice in the world, but some people just have that je ne sais quoi that makes all their code beautiful 😍. my sister's husband is like that. i'm still really new to this stuff -- at this stage I'm happy if I can apply what I've learned to novel problems!
16th Apr 2023, 1:48 AM
Matt Osmond
Matt Osmond - avatar
+ 2
Matt Osmond , VIBED Elegant code looks nice, clunky code is hard to read. But in the end, code is just a means to an end. I wouldn't be worried too much about code beauty in daily life. If it works and gets the job done, it is a good code.
16th Apr 2023, 2:04 AM
Bob_Li
Bob_Li - avatar
15th Apr 2023, 3:27 AM
Bob_Li
Bob_Li - avatar
+ 1
it deserves it I guess the Python equivalent would be n = int(input()) if sum([n%i for i in map(int, input().strip().split())])>0: print('not ', end='') print('divisible by all')
15th Apr 2023, 10:11 AM
Bob_Li
Bob_Li - avatar