Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: php exam

  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post php exam

    can someone help me in this exam

    The function getRepeats will take a string as input, a string of letters and integers and it should return the number of repeated letters/integers.

    Here is an example

    input: AA12457SD722
    output: 3

    Explanation:
    A showed twice
    2 showed 3 times
    7 showed twice

    The function should return 3, meaning there are three unique letters (or integers) showed more than once.

    Important note: the function should be case insensitive, i.e A = a.

    Example:

    input: a123A
    output: 1

    Explanation:
    The letter a showed twice.


    function getRepeats ($str){

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    419
    Thanks
    18
    Thanked 26 Times in 26 Posts
    PHP Code:
    function getRepeats($str)
    {
        
    #Split the string into an array, letter by letter
        
    $a str_split($str);
        
    #Remove any duplicates
        
    array_unique($a);
        
    #Get the count and return
        
    return count($a);

    Last edited by Dubz; 04-27-2014 at 11:52 AM. Reason: Wrong function, used array_diff instead of array_unique

  • #3
    Regular Coder
    Join Date
    Oct 2004
    Posts
    363
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Use strtolower to make the comparisons case insensitive. Split the string and use array_count_values to get an array with the character as the array key and the number of occurrences as the value. Then you just need to remove the single occurrences (value = 1, you could use array_diff) and count the remainder.

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Posts
    419
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Yes, the first line in that function should be $str = strtolower($str);, I didn't notice you said case insensitive. It should work with that though. I guess this is what happens when you don't go to bed when you should ;P

    I also didn't notice you only want it for letters as well. This could be solved with a simple regular expression. Result:
    PHP Code:
    function getRepeats($str)
    {
        
    #Case insensitive
        
    $str strtolower($str);
        
    #Letters only
        
    $str preg_replace("/[^a-z]/"""$str);
        
    #Split the string into an array, letter by letter
        
    $a str_split($str);
        
    #Remove any duplicates
        
    $a array_unique($a);
        
    #Get the count and return
        
    return count($a);

    Last edited by Dubz; 04-28-2014 at 12:51 AM.

  • #5
    Regular Coder
    Join Date
    Oct 2004
    Posts
    363
    Thanks
    0
    Thanked 18 Times in 18 Posts
    You are counting unique characters not repeating characters
    PHP Code:
    <?php

    function getRepeats($string)
    {
        echo 
    '$string = ' $string "\n";
        
    # Make it lowercase as it's case insensitive
        
    $lowercase_string strtolower($string);
        echo 
    '$lowercase_string = ' $lowercase_string "\n";
        
    # Split the string into an array
        
    $characters str_split($lowercase_string);
        echo 
    '$characters = ' "\n"print_r($characters);
        
    # Count the occurrences of each value
        
    $values array_count_values($characters);
        echo 
    '$values = ' "\n"print_r($values);
        
    # Remove any non repeating values
        
    $repeat_values array_diff($values, array(1));
        echo 
    '$repeat_values = ' "\n"print_r($repeat_values);
        
    # Return the number of repeating values
        
    $count count($repeat_values);
        echo 
    '$count = ' $count "\n";
        return 
    $count;
    }

    header('Content-Type: text/plain');
    $repeats getRepeats('AA12457SD722');
    echo 
    'getRepeats returns ' $repeats "\n\n";
    $repeats getRepeats('A123a');
    echo 
    'getRepeats returns ' $repeats;
    Code:
    $string = AA12457SD722
    $lowercase_string = aa12457sd722
    $characters = 
    Array
    (
        [0] => a
        [1] => a
        [2] => 1
        [3] => 2
        [4] => 4
        [5] => 5
        [6] => 7
        [7] => s
        [8] => d
        [9] => 7
        [10] => 2
        [11] => 2
    )
    $values = 
    Array
    (
        [a] => 2
        [1] => 1
        [2] => 3
        [4] => 1
        [5] => 1
        [7] => 2
        [s] => 1
        [d] => 1
    )
    $repeat_values = 
    Array
    (
        [a] => 2
        [2] => 3
        [7] => 2
    )
    $count = 3
    getRepeats returns 3
    
    $string = A123a
    $lowercase_string = a123a
    $characters = 
    Array
    (
        [0] => a
        [1] => 1
        [2] => 2
        [3] => 3
        [4] => a
    )
    $values = 
    Array
    (
        [a] => 2
        [1] => 1
        [2] => 1
        [3] => 1
    )
    $repeat_values = 
    Array
    (
        [a] => 2
    )
    $count = 1
    getRepeats returns 1

  • #6
    Regular Coder
    Join Date
    Sep 2011
    Posts
    419
    Thanks
    18
    Thanked 26 Times in 26 Posts
    I see what he meant now, he wants characters that have duplicates in the string (case insensitive).
    The function I created simply returned bow many different letters there were (case insensitive) rather than that.

    That being said I should probably not give advice when I haven't slept in a while, I can't read things straight sometimes.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Dubz View Post
    I see what he meant now, he wants characters that have duplicates in the string (case insensitive).
    The function I created simply returned bow many different letters there were (case insensitive) rather than that.

    That being said I should probably not give advice when I haven't slept in a while, I can't read things straight sometimes.
    So based on what you originally posted modified to meet the requirements you originally missed:

    Code:
    function getRepeats($str)
    {
        
        $a = str_split(strtolower($str));
        $b = count($a);
        array_unique($a);
        return $b - count($a);
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    Regular Coder
    Join Date
    Oct 2004
    Posts
    363
    Thanks
    0
    Thanked 18 Times in 18 Posts
    That only works for doubles. AA12457SD722 results in 12-8=4 because of the triple 2.
    PHP Code:
    <?php

    function getRepeats($string)
    {
        
    $lowercase_string strtolower($string);
        
    $values count_chars($lowercase_string1);
        
    $repeat_values array_diff($values, array(1));
        return 
    count($repeat_values);
    }
    count_chars makes it a little more simple. There's probably a better way.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •