₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,026 members, 8,419,990 topics. Date: Thursday, 04 June 2026 at 08:55 AM

Toggle theme

Coding Challenge For Fun - Programming (2) - Nairaland

Nairaland ForumScience/TechnologyProgrammingCoding Challenge For Fun (5189 Views)

1 2 Reply (Go Down)

Re: Coding Challenge For Fun by 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 download 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...
Re: Coding Challenge For Fun by 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...
Re: Coding Challenge For Fun by 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.
1 2 Reply

A 30 Day Coding Challenge (Python) For BEGINNERSMonthly Coding Challenge For Developers With Reward January EditionMonthly Coding Challenge For Developers With Reward234

Can Someone Learn Programming On His Own?Porting PHP/MYSQL Applications to Micro-Controller Based DevicesPlease Can Someone Recommend To Me A Low Budget Laptop For Coding