NewStats: 3,265,279 , 8,186,227 topics. Date: Saturday, 14 June 2025 at 08:28 AM 156029

6382y

Coding Challenge For Fun - Programming (2) - Nairaland 736eb

Coding Challenge For Fun (4990 Views)

(4)

Go Down)

efosky1246(m): 10:20am On Oct 14, 2017
young02:
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"wink)
That's all... can attachment or visit link below if code is not well formatted...

https://gist.github.com/nny326/93d34e5d63b023d17aa5fa4534a9fb4a#file-duplicatecounter-py

Bro this might work but it's quite inefficient.why looping thrice...
young02(m): 5:49pm On Oct 15, 2017
efosky1246:


Bro this might work but it's quite inefficient.why looping thrice...

quite inefficient... see as u talk am self...
No be for FUN? tsk...

If point-and-kill is what u prefer, have it!

def counter(word):
word = list(word.lower())
for i in word:
if (i.isalpha()) or (i.isdigit()):
count = word.count(i)
if (count > 1):
print "{} ={}" %(i,count)
while(word.count(i) > 1):
word.remove(i)

that's all....
code attached...

timtoday: 5:42am On Oct 16, 2017
edicied:

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Details {

public void countDupChars(String str){


Map<Character, Integer> map = new HashMap<Character, Integer>();

char[] chars = str.toCharArray();

for(Character ch:chars){
if(map.containsKey(ch)){
map.put(ch, map.get(ch)+1);
} else {
map.put(ch, 1);
}
}

Set<Character> keys = map.keySet();


for(Character ch:keys){
if(map.get(ch) > 1){
System.out.println("Char "+ch+" "+map.get(ch));
}
}
}

public static void main(String a[]){
Details obj = new Details();
obj.countDupChars("hello"wink;

}
}


Better than the previous java solution, but if all the character is not in the same case, your solution would fail for entry with same character but different cases I.e lower and upper cases which are supposed to count as same.

Reply)

Let's Talk About OpenAI ChatGPT. What's The Future of Developers ?

(Go Up)

Sections: How To . 12
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or s on Nairaland.