To check wheather n is formed from m | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

To check wheather n is formed from m

m in given as input.n is given as input. the program has to check wheather m is formed from the elements of n.. for eg 1 m=12345 n=45321 result = yes eg 2: m= 3456 n= 98 result =no

18th Aug 2018, 6:35 AM
Gokula Krishnan S
Gokula Krishnan S - avatar
16 Answers
+ 2
Drunken Data as far as i can see your code doesn't take into account that they might be of different length, so as long as m contains n, your code will output yes. i'm about to see if i can make my own solution
18th Aug 2018, 7:38 AM
hinanawi
hinanawi - avatar
18th Aug 2018, 7:40 AM
hinanawi
hinanawi - avatar
+ 2
print(all([i in m for i in set(n)]))
18th Aug 2018, 10:51 AM
Louis
Louis - avatar
18th Aug 2018, 7:31 AM
hinanawi
hinanawi - avatar
+ 1
m='565' n='555' m={i:m.count(i) for i in m} n={i:n.count(i) for i in n} print(all([m[i]<=n.get(i,0) for i in set(m)]))
18th Aug 2018, 2:51 PM
Louis
Louis - avatar
0
that's easy, you can just sort both and then check if they're the same
18th Aug 2018, 7:07 AM
hinanawi
hinanawi - avatar
0
no for inputs like m=5665 n=555 not coming coz n has three 5's while m has two 5's the output should be no but printing yes
18th Aug 2018, 7:27 AM
Gokula Krishnan S
Gokula Krishnan S - avatar
0
Drunken Data it shouldn't print yes, because when you sort both they're 5566 and 555 respectively which aren't equal. link your code please
18th Aug 2018, 7:29 AM
hinanawi
hinanawi - avatar
0
I don't how to code with the method you sujjested but I have written a code in another way I will link up that
18th Aug 2018, 7:30 AM
Gokula Krishnan S
Gokula Krishnan S - avatar
18th Aug 2018, 7:35 AM
Gokula Krishnan S
Gokula Krishnan S - avatar
0
this is what I have written
18th Aug 2018, 7:36 AM
Gokula Krishnan S
Gokula Krishnan S - avatar
0
pls check for your own inputs too
18th Aug 2018, 7:36 AM
Gokula Krishnan S
Gokula Krishnan S - avatar
0
no this is not for input like m= 1234 n=23 are not working it's printing no but it should print yes because every digit in n is present in m so that n can be formed from m this is what I have to check
18th Aug 2018, 8:02 AM
Gokula Krishnan S
Gokula Krishnan S - avatar
0
Drunken Data oh in that case your solution will work if you iterate the other way around
18th Aug 2018, 8:12 AM
hinanawi
hinanawi - avatar
0
yeah it work for some test cases but in some cases like m=565 n=555 it's printing yes but it should be no coz in n it has three 5 but in m it has only two 5 .my program checks only whether the number in n is present in m but it's not checking for every digit ( espeacially for repeating digits)
18th Aug 2018, 8:16 AM
Gokula Krishnan S
Gokula Krishnan S - avatar
0
I hope you understand what I am trying to say....yeah????
18th Aug 2018, 8:17 AM
Gokula Krishnan S
Gokula Krishnan S - avatar