+ 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
31 Respuestas
+ 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
+ 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.
+ 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 ?
+ 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.
+ 3
+ 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.
+ 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
+ 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
+ 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! 🙌🙌🙌
+ 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.
+ 2
Bob_Li thank you for the praise :)
+ 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... 😅
+ 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.
+ 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! 🙏🙏
+ 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.
+ 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!
+ 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.
+ 1
+ 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')