Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Wee Question

  • 16-07-2007 02:38PM
    #1
    Closed Accounts Posts: 1,981 ✭✭✭


    Psuedo-code or in java or c++ to take an int array of letters and give you the max consecutive occurence of those letters in a row. (assume theres only 2 letters, a and b)

    e.g int array = aaabbbbbbaabbbaaabbb

    In this case the max would be 6 since we have 6 b's in a consecuative row.

    Anyone give this a shot ?


Comments

  • Registered Users, Registered Users 2 Posts: 5,333 ✭✭✭Cake Fiend


    How about you show what you've tried so far?


  • Registered Users, Registered Users 2 Posts: 6,790 ✭✭✭cornbb


    How can your array hold characters if you're declaring int as the datatype?


  • Registered Users, Registered Users 2 Posts: 85 ✭✭slavigo


    
    # input list of characters
    all_chars = 'aaabbbbbbaabbbaaabbb'
    
    # placeholder for current character and it's count
    current_char = ''
    current_count = 0
    
    # placeholder for biggest occurrence of a character so far
    # and it's count
    biggest_char = ''
    biggest_count = 0
    
    # for each character
    for char in all_chars:
            # If first character or a different character to the
            # character we are currently counting
            if not current_char or not char == current_char:
                    # If we've met a new character and the last run of chars
                    # was longer than the previously recorded longest run
                    if current_count > biggest_count:
                            # Save both character and count
                            biggest_char = current_char
                            biggest_count = current_count
                    # Save the new character and start a new occurrences count
                    current_char = char
                    current_count = 1
            # Else it's the same character again
            else:
                    # So increment our current running total
                    current_count = current_count + 1
    # if count of occurrences at the end of the input list of characters
    # is the longest count for a character. Save it.
    if current_count > biggest_count:
            biggest_char = current_char
            biggest_count = current_count
    
    # Print out our results.
    print biggest_char
    print biggest_count
    
    

    Here you go. It's written in python. Kinda like pseudo code I suppose but if you've got python, a little bit of copy and paste will have it running. Oh, one thing this example doesn't allow for is a case where two counts of the biggest occurrences are the same. I.e. if a and b both had 6 occurrences. This program would return the first character of that length that it met.

    Hope this helps.


  • Registered Users, Registered Users 2 Posts: 221 ✭✭Elfman


    hmm top of my head :

    while(array.hasanotherelement()){


    if (array[cnt] == array[cnt+1]){
    concnter++

    }
    else{
    if(currentlrgnumber < concnter){

    currentlrgnumber = concnter;
    }//if

    concntere = 0;

    }//else


    }//while

    system.out.println("largest num is " + currentlrgnumber);


    this is a rough guide to the logic. there some itialisatioin to be done but sin e.


  • Registered Users, Registered Users 2 Posts: 85 ✭✭slavigo


    Cake Fiend wrote:
    How about you show what you've tried so far?

    Sorry Cake Fiend, Just read this:
    "Homework & Learning to code: How to ask a question! (The Rules)"
    I see where you were going with your request now.
    Which of coarse was in the right direction.

    My bad but it won't happen again.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 5,333 ✭✭✭Cake Fiend


    cornbb wrote:
    How can your array hold characters if you're declaring int as the datatype?

    Characters are stored in memory as ascii values (i.e. small integers)


Advertisement