Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,502 members, 7,819,826 topics. Date: Tuesday, 07 May 2024 at 01:31 AM

Coding Challenge For Fun - Programming - Nairaland

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

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


input = "aA11"
input = input.lower()
list = [0] * 36
for i in input:
if i.isdigit():
list[int(i)+26]+=1
else:
list[ord(i)-97]+=1
i=0
counter = 0
while i < len(input):
if input[i].isdigit():
counter += list[int(input[i])+26]-1
i += list[int(input[i])+26]
else:
counter += list[ord(input[i])-97]-1
i += list[ord(input[i])-97]
print counter


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:
^^^ With the above code, you can easily use as follow
.
.
.
int countWord = counter("java"wink;
// answer will give you 1
check if d code is correct with ideon na
Re: Coding Challenge For Fun by edicied: 11:23pm On Aug 12, 2017
Javanian:
Python


input = "aA11"
input = input.lower()
list = [0] * 36
for i in input:
if i.isdigit():
list[int(i)+26]+=1
else:
list[ord(i)-97]+=1
i=0
counter = 0
while i < len(input):
if input[i].isdigit():
counter += list[int(input[i])+26]-1
i += list[int(input[i])+26]
else:
counter += list[ord(input[i])-97]-1
i += list[ord(input[i])-97]
print counter


https://ideone.com/UstREc
wHEN I TESTED YOUR CODE IT GAVE ME THIS....


>>> >>> >>> ... ... ... ... ... File "<stdin>", line 6
i=0
^
SyntaxError: invalid syntax
>>> >>> ... ... ... ... ... ... ... File "<stdin>", line 8
print counter
^
SyntaxError: Missing parentheses in call to 'print'
>>>
Re: Coding Challenge For Fun by edicied: 11:28pm On Aug 12, 2017
SilverG33k:

//pardon me if the code is wrong but trust me, the logic is correct
You know its not actuall to find the length of the word but to find duplicate letters in the word tongue
Re: Coding Challenge For Fun by antieverything(m): 11:32pm On Aug 12, 2017

bash!
## yay bash is a juke box!
number_caught=0 # yap ..jukebox ..global variable
give_me_num () {
word= $1
lera= $2
number_caught= $(echo $word | awk '{print tolower($0)}' | grep -o "lera" | wc -l)
# ok pls remember number_caught is global plss plss plss
}

give_me_num "yup life in programming is " "r"
echo $caught_num
Re: Coding Challenge For Fun by edicied: 11:39pm On Aug 12, 2017
antieverything:

bash!
## yay bash is a juke box!
number_caught=0 # yap ..jukebox ..global variable
give_me_num () {
word= $1
lera= $2
number_caught= $(echo $word | awk '{print tolower($0)}' | grep -o "lera" | wc -l)
# ok pls remember number_caught is global plss plss plss
}

give_me_num "yup life in programming is " "r"
echo $caught_num
Is this for linux Terminal??
Re: Coding Challenge For Fun by Javanian: 11:15am On Aug 13, 2017
edicied:

wHEN I TESTED YOUR CODE IT GAVE ME THIS....


>>> >>> >>> ... ... ... ... ... File "<stdin>", line 6
i=0
^
SyntaxError: invalid syntax
>>> >>> ... ... ... ... ... ... ... File "<stdin>", line 8
print counter
^
SyntaxError: Missing parentheses in call to 'print'
>>>


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:

You know its not actuall to find the length of the word but to find duplicate letters in the word tongue
Finding the lenght is easy as word.lenght() but my code actually finds if the letters appear twice
e.g
counter("java"wink; // 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:


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.
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:

Finding the lenght is easier as word.lenght() but my code actually finds if the letters appear twice
e.g
counter("java"wink; // will return 1
according to what you asked, a as the only letter appears twice
It suppose to return 2
Re: Coding Challenge For Fun by Javanian: 12:24pm On Aug 13, 2017
edicied:

No, i didn't screw up the indentation and yes i also run it on ideone the link you posted and xame error

What exactly are you talking about?

Re: Coding Challenge For Fun by edicied: 2:04pm On Aug 13, 2017
Javanian:


What exactly are you talking about?
ok cool
Re: Coding Challenge For Fun by SilverG33k(m): 2:30pm On Aug 13, 2017
edicied:
It suppose to return 2
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:

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 ??

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:
Python


input = "aA11"
input = input.lower()
list = [0] * 36
for i in input:
if i.isdigit():
list[int(i)+26]+=1
else:
list[ord(i)-97]+=1
i=0
counter = 0
while i < len(input):
if input[i].isdigit():
counter += list[int(input[i])+26]-1
i += list[int(input[i])+26]
else:
counter += list[ord(input[i])-97]-1
i += list[ord(input[i])-97]
print counter


https://ideone.com/UstREc

This works accurately,

1 Like

Re: Coding Challenge For Fun by edicied: 4:30pm On Aug 13, 2017
SilverG33k:

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 ??
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:


This works accurately,
Yeah it does
Re: Coding Challenge For Fun by SilverG33k(m): 4:34pm On Aug 13, 2017
edicied:

I ment something like a = 2 and not the return statement
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:

Yeah it does

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" grin
Re: Coding Challenge For Fun by orimion(m): 12:15am On Aug 14, 2017
pcguru1:


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

please when you guys post question, always state the instructions clearly and give good examples

according to the instruction
edicied:
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'

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

java
should return 1 because 'a' is the only character occuring more than once

if we go by your reasoning that
a
occurs 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

let input = "aA11";
let [o, n, c] = [{}, {}, 0];
input = input.toLowerCase();
for (let i = 0, ch; i < input.length; i += 1) {
ch = input[i];
if (o[ch] && !n[ch]) {
c += 1;
n[ch] = 1;
} else o[ch] = 1;
}

console.log(c); // => count

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:

Is this for linux Terminal??
yeah!
Re: Coding Challenge For Fun by OmotayoOlawoye(m): 11:46am On Aug 16, 2017
edicied:
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'

# 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:
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'
Java

public class Result {
private int numDist;
private List<String> charsRepeated = new ArrayList<String>();

//Getters and setters skipped, but assume they have been defined.
}

---Separate file.
public class Naira {
public static int numDistinctChars(String alphaNumWord) {
Result res = new Result();
String[] chars = alphaNumWord.toCharArray();
Set<String> distinctChars = new TreeSet<String>();
for (String char : chars) {
if (!distinctChars.add(char)) {
res.getCharsRepeated().add(char);
}
}
res.setNumDist(distinctChars.size());
}
}

//From the consumer.
.... {
.....
Result res = Naira.numDistinctChars("dsds323212" ) ;
....
}
Re: Coding Challenge For Fun by edicied: 12:38am On Oct 14, 2017

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;

}
}

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"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

(1) (2) (Reply)

My Data Analytics Journey / Is PHP Dying? / Help With A Small Qbasic Program

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