[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

New to python - Just a question

On 03/07/2020 11:09, Daley Okuwa via Python-list wrote:
> Please can someone help
> Write an algorithm (choose the language you prefer) that given a character string,?for instance {?c?,?a?,?i?,?o?,?p?,?a?}, will print out the list of characters appearing at least 2 times.?In this specific example, it would return {?a?}. Afterwards, comment out the cost in terms of space and time.

The first thing to do with any problem is to break it down into bits. 
In the case of Python, writing them out as "pseudo-code" instructions 
often helps.  In this case you have:

Set up something to count letters with
For each letter in the string:
     If we haven't seen this letter before:
         Set the counter for the letter to 1
         Add 1 to the counter for the letter

For each counter:
     If the count is 2 or more:
         Print the letter

Now there are a lot of possible ways to write that, but they mostly come 
down to deciding what data structure to use to count letters with.  Have 
a browse through a tutorial (or the standard library if you are feeling 
adventurous) and see what you think might work, then try it.

> Write a bash/python script that takes a directory as an argument and output the total lines of code in *.cpp files recursively.

In bash, what existing commands count things?  If you don't know, how 
might you find out?  Then you have to figure out how to do that for each 
*.cpp file in a directory, and add the totals together.

In Python, you can read a file one line at a time, so how to count the 
number of lines in a file should be pretty obvious.  Figuring out how to 
do that for every *.cpp file in a directory will involve looking through 
the standard library, or getting a bash script to do it for you :-)

Rhodri James *-* Kynesim Ltd