Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,158,078 members, 7,835,637 topics. Date: Tuesday, 21 May 2024 at 12:48 PM

Coding Challenge For Fun - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Coding Challenge For Fun (4605 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)

(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)

How To Make Simple Animation Videos..tips,tricks & Tools / Ambitious Programmers Lets Talk / Remita Integration

(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. 16
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.