0

I was looking for a program to do the elfish instances. Then I found this javascript code on the web.

var containsE;
var containsL; 
var containsF; 

function elfish(str){

    var checkLetter = str[str.length - 1]; 
    if (checkLetter === "e"){
        containsE = true;
    }
    else if (checkLetter === "l"){
        containsL = true; 
    }
    else if (checkLetter === "f"){
        containsF = true; 
    }
    // base case 
    if (str.length === 0)
        if (containsE && containsL && containsF){
            return true; 
        }
        else {
            return false; 
        }
    // if not base case     
    return elfish(str.slice(0, str.length - 1)); 
}

elfish("whiteleaf"); 

I wonder if it is possible to guide me to convert the code into python with an explanation? the python version is 2.73

3
  • Could you explain what the desired output of the code should be? Commented Oct 2, 2019 at 13:35
  • 2
    Try it yourself. If you have a specific question, you can ask here. Commented Oct 2, 2019 at 13:36
  • Hi, so for the output, if the user enter the word waffles then it is a elfish Commented Oct 2, 2019 at 14:31

1 Answer 1

1

I think there is nothing special to explain here. Python is very self-explaining in your case. To keep it readable i added parenthesis in the if statement. You could just leave them:

while True:
    # type in which word you want
    s = input() 
    # if some letter in your string was found
    if ('e' in s) and ('l' in s) and ('f' in s): 
        print('true')
    else:
        print('false')
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the guidance and the explanation!
If this solved your question i would be glad if you mark it as answered. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.