Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,540 members, 7,955,010 topics. Date: Saturday, 21 September 2024 at 02:22 PM |
Nairaland Forum / Science/Technology / Programming / Coding Challenge For Fun (4734 Views)
A 30 Day Coding Challenge (Python) For BEGINNERS / Monthly Coding Challenge For Developers With Reward January Edition / Monthly Coding Challenge For Developers With Reward (2) (3) (4)
Coding Challenge For Fun by edicied: 1:40am On Aug 12, 2017 |
Count the number of Duplicates In any Programing Language, Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits. Example "abcde" -> 0 # no characters repeats more than once "aabbcde" -> 2 # 'a' and 'b' "aabBcde" -> 2 # 'a' occurs twice and 'b' twice (bandB) "aA11" -> 2 # 'a' and '1' |
Re: Coding Challenge For Fun by Javanian: 10:17am On Aug 12, 2017 |
Python
https://ideone.com/UstREc |
Re: Coding Challenge For Fun by deedat205(m): 5:03pm On Aug 12, 2017 |
Python 3 def solve(z): p,count = z.upper(),0 for b in set(p): if p.count(b) > 1: count+= 1 return count s = 'aabBcde' print(solve(s)) https://ideone.com/DhTkdh https://ideone.com/DhTkdh 1 Like |
Re: Coding Challenge For Fun by SilverG33k(m): 7:14pm On Aug 12, 2017 |
[Modified] Java... public int counter(String word){ int count = 0; //this with count if letter or number occur twice int wordCounter = 0; //this will let us know how many times a letter occur for (int i = 1; i<word.length(); i++) { String letter = String.valueOf(word.charAt(i)); //pick each letter of the word for (int y = 0; y < word.length(); y++) { //then check how many times that picked letter occur if (String.valueOf(word.charAt(i)).equals(letter)) { wordCounter++; //count the letter occurence if (wordCounter == 2) { count++; // if the letter occurs twice, we count } } } } //then what we count earlier is returned return count; } Tested OK with android studio, I just quickly tested that since I'm currently on a project and it worked..... 100% 1 Like |
Re: Coding Challenge For Fun by SilverG33k(m): 10:15pm On Aug 12, 2017 |
^^^ With the above code, you can easily use as follow . . . int countWord = counter("java" ) ; // answer will give you 1 |
Re: Coding Challenge For Fun by melodyogonna(m): 10:39pm On Aug 12, 2017 |
SilverG33k:check if d code is correct with ideon na |
Re: Coding Challenge For Fun by edicied: 11:23pm On Aug 12, 2017 |
Javanian:wHEN I TESTED YOUR CODE IT GAVE ME THIS....
|
Re: Coding Challenge For Fun by edicied: 11:28pm On Aug 12, 2017 |
SilverG33k:You know its not actuall to find the length of the word but to find duplicate letters in the word |
Re: Coding Challenge For Fun by antieverything(m): 11:32pm On Aug 12, 2017 |
|
Re: Coding Challenge For Fun by edicied: 11:39pm On Aug 12, 2017 |
antieverything:Is this for linux Terminal?? |
Re: Coding Challenge For Fun by Javanian: 11:15am On Aug 13, 2017 |
edicied: What interpreter are you using? Hope you are not screwing up the indentation? It runs on ideone, the link I pasted. Tested on 3 different interpreters online and couldn't replicate this error. |
Re: Coding Challenge For Fun by SilverG33k(m): 12:06pm On Aug 13, 2017 |
edicied:Finding the lenght is easy as word.lenght() but my code actually finds if the letters appear twice e.g counter("java" // will return 1 according to what you asked, a is the only letter appearing twice |
Re: Coding Challenge For Fun by edicied: 12:12pm On Aug 13, 2017 |
Javanian:No, i didn't screw up the indentation and yes i also run it on ideone the link you posted and xame error |
Re: Coding Challenge For Fun by edicied: 12:12pm On Aug 13, 2017 |
SilverG33k:It suppose to return 2 |
Re: Coding Challenge For Fun by Javanian: 12:24pm On Aug 13, 2017 |
edicied: What exactly are you talking about?
|
Re: Coding Challenge For Fun by edicied: 2:04pm On Aug 13, 2017 |
Javanian:ok |
Re: Coding Challenge For Fun by SilverG33k(m): 2:30pm On Aug 13, 2017 |
edicied:Why are you confusing everything, you said it yourself "aabbcde" -> 2 "aabBcde" -> 2 "aA11" -> 2 Then how the Bleep is "Java" supposed to return 2 ?? |
Re: Coding Challenge For Fun by Nobody: 2:43pm On Aug 13, 2017 |
SilverG33k: Number of occurrence a appears twice in J(a)v(a) this is a codewars question. You read the question properly before answering online interviews are strict like that |
Re: Coding Challenge For Fun by Nobody: 2:46pm On Aug 13, 2017 |
Javanian: This works accurately, 1 Like |
Re: Coding Challenge For Fun by edicied: 4:30pm On Aug 13, 2017 |
SilverG33k:I ment something like a = 2 and not the return statement 1 Like |
Re: Coding Challenge For Fun by edicied: 4:31pm On Aug 13, 2017 |
pcguru1:Yeah it does |
Re: Coding Challenge For Fun by SilverG33k(m): 4:34pm On Aug 13, 2017 |
edicied:pcguru1 and op, I thought he was talking about the return variable,,,, sorry mybad |
Re: Coding Challenge For Fun by Nobody: 8:37pm On Aug 13, 2017 |
edicied: The original comment was it wasn't working i thought i copied the other code but i copied your own code by mistake, so had no choice than to write "it works acurately" |
Re: Coding Challenge For Fun by orimion(m): 12:15am On Aug 14, 2017 |
pcguru1: please when you guys post question, always state the instructions clearly and give good examples according to the instruction edicied: the count of distinct character occuring more than once in all the examples given, the returned value was the number of character occuring more than once (not the number of times they occur!) javashould return 1 because 'a' is the only character occuring more than once if we go by your reasoning that aoccurs twice in java, what is going to be the returned value for aabbbccccddddd? (check the examples given in the op before answering) [edit] admittedly, the question did say the COUNT of the characters occuring more than once but the examples given did not reflect the instruction. you didnt provide a correct returned value for any (but the first) of the examples 1 Like |
Re: Coding Challenge For Fun by Kodejuice: 4:51am On Aug 14, 2017 |
O(N) time JS
1 Like |
Re: Coding Challenge For Fun by harryobas: 9:34am On Aug 16, 2017 |
In Ruby: def duplicate_count(word) characters = word.downcase.split('') characters.select{|c| characters.count(c) > 1}.uniq.count end |
Re: Coding Challenge For Fun by antieverything(m): 10:50am On Aug 16, 2017 |
edicied:yeah! |
Re: Coding Challenge For Fun by OmotayoOlawoye(m): 11:46am On Aug 16, 2017 |
edicied: # PYTHON program # this function handles the counting of the number of # occurrence of each letter. def countOccurrence(word): WordSet = set([]) myDict = {} for c in word: WordSet.add(c) for letter in WordSet: myDict.update({letter : word.count(letter)}) return myDict # Example words. word1 = 'hippopotamus' word2 = 'hippopotomonstrosesquipedaliophobia' word3 = 'pneumonoumtramicroscopicsilicovocanokoniosis' # create different dictionary type variables that takes the # dictionary returned from the method call. dict1 = countOccurrence(word1) dict2 = countOccurrence(word2) dict3 = countOccurrence(word3) print (dict1, '\n' ) print (dict2, '\n' ) print (dict3, '\n' ) # the following allows the user to enter the word they want the # number of occurrence of each letter computed. # this will prompt the user to enter a word. new_word = input ("Enter a word: " ) new_dict = countOccurrence(new_word) print (new_dict, '\n') 1 Like |
Re: Coding Challenge For Fun by Nobody: 3:58pm On Aug 18, 2017 |
Special respect to all the programmers here. JavaScript version <script type="text/javascript"> <!-- var str="Abracadabra"; //source Talabi Olabode var times=str.match(/a/gi).length;// gives the frequency for A var Bleep=str.match(/r/gi).length;// gives the frequency for R document.write("The number of times "A" appears is:"+times+"<br />'); document.write("The number of times "R" appears is:"+Bleep); //--> </script> |
Re: Coding Challenge For Fun by Nairaface: 9:21pm On Aug 19, 2017 |
edicied:Java public class Result { ---Separate file. public class Naira { //From the consumer. .... { |
Re: Coding Challenge For Fun by edicied: 12:38am On Oct 14, 2017 |
|
Re: Coding Challenge For Fun by young02(m): 8:36am On Oct 14, 2017 |
solution in Python(2) <code> def counter(word): numberList = [] # empty list for numbers alphaList = [] # empty list for alphabets #sorting num and alpha from word into resp. list for ch in word: if ch.isdigit(): numberList.append(ch) elif ch.isalpha(): alphaList.append(ch.lower()) #converting to lower case since matching is case-INsensitive #counting and print only num/alpha occurring more than once for x in numberList: count = numberList.count(x) if count >1: print "{} --> {} ".format(x,count) while numberList.count(x) > 1 : #weeding tested num numberList.remove(x) for i in alphaList: count = alphaList.count(i)and if count > 1: print" {} --> {}".format(i, count) while alphaList.count(i) > 1: #weeding tested alpha alphaList.remove(i) </code> call function with your string as argument e.g counter("pEpper62532622") That's all... can download attachment or visit link below if code is not well formatted... https://gist.github.com/nny326/93d34e5d63b023d17aa5fa4534a9fb4a#file-duplicatecounter-py
|
Ambitious Programmers Lets Talk / Rynga Voip Issues - The Call Failed Due To Sound Device Problem On This Computer / Web Development Isn't Lucrative Anymore.
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 55 |