Question

In python, please help me fill in these functions given their limitations. Please use for loops....

In python, please help me fill in these functions given their limitations. Please use for loops.

def palindrome(x):

"""

For a given string, determine if the string is a palindrome. (The same forward and back)

Input: String of any size (including empty)

Return: Boolean (True if palindrome, False otherwise)

Limitation: You must use a loop. Only function allowed to be used is len (if needed). Cannot use "in" besides "for var in container"

"""

pass


def getCount(char, strng):

"""

Get the number of times a character appears in a string.

Input: A character (length 1) and a string of any length

Return: Number of times a character appears

Limitation: Only functions we have been taught in class. Must use a loop. Cannot use "in" besides "for var in container"

"""

string = input("What word do you want to use? >>")

char = input("What letter do you want the number of? >>")

for char in string:

if string[1:] == char

x+=1

def getIndex(char, strng):

"""

Gets the index of the character in the string

Input: Character of length 1 and a string of any length

Return: The first occurence of the character appearing in the string.

Return the length of the string if the character does not exist

Limitation: Only functions we have been taught in class. Must use a loop. Cannot use "in" besides "for var in container"

"""

pass

def areEqual(str1, str2):

"""

Determines if 2 strings are equal

Input: 2 strings of any length (Both can be different lengths)

Return: Boolean

Limitation: Must use a single loop, and only 1 return statement in the code. Cannot use "in" besides "for var in container"

"""

pass

print("Palindrome")

print("Actual: ", palindrome("aba"), " Expected: ", True)

print("Actual: ", palindrome("a"), " Expected: ", True)

print("Actual: ", palindrome("abba"), " Expected: ", True)

print("Actual: ", palindrome("amanaplanacanalpanama"), " Expected: ", True)

print("Actual: ", palindrome("abca"), " Expected: ", False)

print("Actual: ", palindrome("ac"), " Expected: ", False)

print("Actual: ", palindrome("adabbba"), " Expected: ", False)

print("Actual: ", palindrome("amandaplanacanalpanama"), " Expected: ", False)

print()

print("Total Count")

print("Actual: ", getCount("t", "Tomato"), "Expected: ", 1)

print("Actual: ", getCount("a", "supercalifragilisticexpialidocious"), "Expected: ", 3)

print("Actual: ", getCount(" ", "The tomato is not a vegatable but a fruit. Into the salad"), "Expected: ", 11)

print()

print("Get Index")

print("Actual: ", getIndex("t", "Tomato"), "Expected: ", 4)

print("Actual: ", getIndex("a", "supercalifragilisticexpialidocious"), "Expected: ", 6)

print("Actual: ", getIndex(" ", "The tomato is not a vegatable but a fruit. Into the salad"), "Expected: ", 3)

print()

print("Are Equal")

s1 = "Tomato"

s2 = "Potato"

s3 = "Jotato"

s4 = "Lotato"

print("Actual: ", areEqual(s4, s4), "Expected: ", True)

print("Actual: ", areEqual(s1, s2), "Expected: ", False)

print("Actual: ", areEqual(s3, s2), "Expected: ", False)

print("Actual: ", areEqual(s1, s1), "Expected: ", True)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Here is the completed code for this problem.  Let me know if you have any doubts or if you need anything to change.

Thank You !!

=================================================================================================
def palindrome(x):
    reverse_x=''
    for i in range(len(x)):
        reverse_x=x[i]+reverse_x
    return reverse_x==x

def getCount(char, strng):

    count=0
    for i in range(len(strng)):
        if strng[i]==char:
            count+=1
    return count

def getIndex(char, strng):

    for i in range(len(strng)):
        if strng[i]==char:
            return i
    return len(strng)


def areEqual(str1, str2):
    if len(str1) is not len(str2):
        return False
    for i in range(len(str1)):
        if str1[i] is not str2[i]:
            return False
    return True

print("Palindrome")
print("Actual: ", palindrome("aba"), " Expected: ", True)
print("Actual: ", palindrome("a"), " Expected: ", True)
print("Actual: ", palindrome("abba"), " Expected: ", True)
print("Actual: ", palindrome("amanaplanacanalpanama"), " Expected: ", True)
print("Actual: ", palindrome("abca"), " Expected: ", False)
print("Actual: ", palindrome("ac"), " Expected: ", False)
print("Actual: ", palindrome("adabbba"), " Expected: ", False)
print("Actual: ", palindrome("amandaplanacanalpanama"), " Expected: ", False)
print()
print("Total Count")
print("Actual: ", getCount("t", "Tomato"), "Expected: ", 1)
print("Actual: ", getCount("a", "supercalifragilisticexpialidocious"), "Expected: ", 3)
print("Actual: ", getCount(" ", "The tomato is not a vegatable but a fruit. Into the salad"), "Expected: ", 11)
print()
print("Get Index")
print("Actual: ", getIndex("t", "Tomato"), "Expected: ", 4)
print("Actual: ", getIndex("a", "supercalifragilisticexpialidocious"), "Expected: ", 6)
print("Actual: ", getIndex(" ", "The tomato is not a vegatable but a fruit. Into the salad"), "Expected: ", 3)
print()
print("Are Equal")
s1 = "Tomato"
s2 = "Potato"
s3 = "Jotato"
s4 = "Lotato"
print("Actual: ", areEqual(s4, s4), "Expected: ", True)
print("Actual: ", areEqual(s1, s2), "Expected: ", False)
print("Actual: ", areEqual(s3, s2), "Expected: ", False)
print("Actual: ", areEqual(s1, s1), "Expected: ", True)

=================================================================================================

thanks!

Add a comment
Know the answer?
Add Answer to:
In python, please help me fill in these functions given their limitations. Please use for loops....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone please help me with this javascript - Javascript - do at least 10 of...

    Can someone please help me with this javascript - Javascript - do at least 10 of the following questions below ----------------------------------------------------------------------------------- Create an HTML file that takes in input and carries out of the following functions and manipulates the DOM to show the outcome. Please put the question itself as a comment above each answer. Use either JavaScript, jQuery, or both (but know the difference!). ----------------------------------------------------------------------------------- 1. Fibonacci Define function: fib(n) Return the nth number in the fibonacci sequence. 2....

  • [3] In python, please help me write the functions needed for this problem: Another Way to do Parentheses: For this probl...

    [3] In python, please help me write the functions needed for this problem: Another Way to do Parentheses: For this problem, you will be given a string that contains parentheses, brackets, and curly braces ( (, ), [, ], {,} ) and other characters. As a human you are given a string of any characters, determine if the parentheses, brackets, and braces match. (No trailing or leading parentheses). The function called on an empty string will return True. Parentheses must...

  • Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include...

    Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...

  • Can anyone help me fix this program to make sure use stack and use STL library...

    Can anyone help me fix this program to make sure use stack and use STL library to check the name1 and name2 is palindrome. Thank you stackPalindrome.h #ifndef_STACK_PALINDROME_H #define_STACK_PALINDROME_H template <class StackPalindrome> class StackPalindrome { public:    virtual bool isEmpty() const = 0;    virtual bool push(const ItemType& newEntry) = 0;    virtual bool pop() = 0;    virtual ItemType peek() const = 0; }; #endif stack.cpp #include<iostream> #include<string> #include<new> #include "stackPalindrome.h" using namespace std; template <class ItemType> class OurStack...

  • Can you help me make these two methods listed below work? I have code written but...

    Can you help me make these two methods listed below work? I have code written but it is not working. I would appreciate any advice or help. Function "isAPalidrome" accepts an array of characters and returns an integer. You must use pointer operations only, no arrays. This function should return 1 (true) if the parameter 'string' is a palindrome, or 0 (false) if 'string' is not a palindrome. A palindrome is a sequence of characters which when reversed, is the...

  • In C++ please! *Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!* /** CS 150 C-Strings Follow the instructions on your handout to complete t...

    In C++ please! *Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!* /** CS 150 C-Strings Follow the instructions on your handout to complete the requested function. You may not use ANY library functions or include any headers, except for <cstddef> for size_t. */ #include <cstddef> // size_t for sizes and indexes ///////////////// WRITE YOUR FUNCTION BELOW THIS LINE /////////////////////// ///////////////// WRITE YOUR FUNCTION ABOVE THIS LINE /////////////////////// // These are OK after...

  • containsSubSequence takes two Strings as input and returns a boolean: Returns true if the first input...

    containsSubSequence takes two Strings as input and returns a boolean: Returns true if the first input string contains all the characters of the second input string, in order, but not necessarily consecutively. > HW2.containsSubSequence("abracadabra", "abcd") true > HW2.containsSubSequence("abracadabra", "abdc") false you must not use either break or continue in your code. You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method I write something...

  • Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray();...

    Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray(); char[] reverse = new char[original.length]; for(int i =0; i<s.length(); i++){ reverse[i] = original[original.length-1-i]; } return new String(reverse); } /**  * Takes in a string containing a first, middle and last name in that order.  * For example Amith Mamidi Reddy to A. M. Reddy.  * If there are not three words in the string then the method will return null  * @param name in...

  • I really need help with the code for this, in C++ please, I know there's a...

    I really need help with the code for this, in C++ please, I know there's a similar question but the answer isn't correct :[ A palindrome is a string that reads the same forwards and backwards (ignoring spaces, punctuation, and capitalization). Examples are the familiar ``If I had a hi-fi,'' the grander ``A man, a plan, a canal, Panama,'' and ``Some men interpret nine memos.'' So the goal: Design, implement, document, and test a program that reads a line of...

  • Please read the problem carefully and answer the 2 questions below code: /***************************************************************** * Program: palindrome.c...

    Please read the problem carefully and answer the 2 questions below code: /***************************************************************** * Program: palindrome.c * * Purpose: implements a recursive function for determining *   if a string is a palindrome * * Authors: Steven R. Vegdahl, Tammy VanDeGrift, Martin Cenek * *****************************************************************/ #include #include #include /***************************************************************** * is_palindrome - determines whether a string of characters is a palindrome * * calling sequence: *    result = is_palindrome(str, first_index, last_index) * * parameters - *    str - the string to test *    first_index -...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT