Question

characters) and prints ) 3. Write function, smallestCharacterLargerThan(keyChar, inputString) that takes as input key character, keyChar, and a string of one or more letters (and no other the smallest character in the string that is larger than keyChar, where one character is smaller than another if it occurs earlier in the alphabet (thus C is smaller than y and both are larger than B) and 2) the index of the final occurrence of that character. When comparing letters in this problem, ignore case. L.e. e and E should be treated as equal. However, your output should use the case of the smallest letter as it appears at the index of its final occurrence in the string. Thus, in the example below The smallest character is y.. would be incorrect If no character in the given string is larger than keyChar, print an appropriate message. Important: your function must contain exactly one loop and you must determine the index of the character within the loop (i.e. you may not use Pythons max, min, index, reverse, [::-1], or find, and you may not use any lists). You may, however, use Pythons lower) function to help with string case if you wish. For example, >>> smallestcharacterLargerThan (Х, yRrcDefx8qubs1yJYelakd } should yield something like: In yRreDetxBqubs1yjYelskd, the smallest character greater than xis Y and occurs at position 6

i need python code, thank you very much!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have written the code using PYTHON PROGRAMMING LANGUAGE.

INPUT :

keychar = 'x'

string = "yRrcDefxBqubSlyjYelskd"

OUTPUT:

/TemptingPreciousRedundancy tion run share repl talk Sign ui main.py saved #F unction definition for smallestCharacterLargerThan 2 def smallestCharacterLargerThan(keyChar,inputString): Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linuzx The Smallest character greater than x is Y and occurs at position 16. indexz-1 #defined index variable to store position mini:50 #defined mini variable #for loop for accessing each character of string for i in range(len (inputstring)): 4 #ord is for getting the ascii value for the character value ord(inputstring[i] l()Ford(keyChar.lower()) if(value > Θ and mini)svalue) : 7 9 index i #storing the position #condition to print the result on the console if(index!- -1): 12 print(The Smallest character greater than +keyChar+ is +inputString[index+nand occurs at position +str(index) 13 else: 14 15 16 17 18 smallestCharacterLargerThan(x, yRrcDefxBqubSlyjYelskd) print (There is no Smallest character greater than +keyChar) #Calling smallestCharacterLargerThan function

INPUT:

keychar = 'x'

string = "yRrcDefxBqubSljelskd"

OUTPUT:

88 repl Talk Sign up main.py E saved 1 #Function definition for smallestCharacterLargerThan 2 def smallestCharacterLargerThan (keyChar,inputstring): Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux The Smallest character greater than x is y and occurs at position 0. index--1 #defined index variable to store position mini-se #defined mini variable #for loop for accessing each character of string for i in range(len (inputstring)): 4 6 8 9 18 #ord is for getting the ascii value for the character value ord (inputstring[i].lower))-ord (keyChar.lower()) İf (value > θ and mini)svalue) : index = 1 #storing the position #Condition to print the result on the console if (index! -1): 12 13 print(The Smallest character greater than +keyChar+ is +inputstring[index]+\nand occurs at position +str(index) else: 14 15 16 17 18 print(There is no Smallest character greater than +keyChar) smallestcharacterLargerThan(X,yArcDefxBaubs1jelskd)#Calling smallestCharacterLargerThan function

Please find the snippet of code below ..

CODE:

#Function definition for smallestCharacterLargerThan

def smallestCharacterLargerThan(keyChar,inputString):

index=-1 #defined index variable to store position

mini=50 #defined mini variable

#for loop for accessing each character of string

for i in range(len(inputString)):

#Ord is for getting the ascii value for the character

value = ord(inputString[i].lower())-ord(keyChar.lower())

if(value > 0 and mini>=value):

index = i #storing the position

#Condition to print the result on the console

if(index!= -1):

print("The Smallest character greater than "+keyChar+" is "+inputString[index]+" and occurs at position "+str(index)+".")

else:

print("There is no Smallest character greater than "+keyChar)


smallestCharacterLargerThan("x","yRrcDefxBqubSlyjYelskd")#Calling smallestCharacterLargerThan function

Thanks..

Add a comment
Know the answer?
Add Answer to:
i need python code, thank you very much! characters) and prints ) 3. Write function, smallestCharacterLargerThan(keyChar,...
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
  • Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

    Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...

  • I need eclipse code for : Write a program that analyzes text written in the console...

    I need eclipse code for : Write a program that analyzes text written in the console by counting the number of times each of the 26 letters in the alphabet occurs. Uppercase and lowercase letters should be counted together (for example, both ‘A’ and ‘a’ should count as an A). Any characters that are not letters should be ignored. You must prompt the user to enter the text to be analyzed. Then, for any letter that appeared at least once...

  • PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set...

    PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set consists primarily of numerous, long runs of repeated characters. For example, AAAAAAAAAA is a run of 10 A’s. We could encode this run using a notation like *A10, where the * is a special flag character that indicates a run, A is the symbol in the run, and 10 is the length of the run. As another example, the string KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG would be encoded...

  • Python Write a function named letter_function. The function should accept a string (i.e., any string) and...

    Python Write a function named letter_function. The function should accept a string (i.e., any string) and display the letters in that string. For example, if the function received "python 3.2". The following letters will be displayed: (Hint: you can use s.isalpha() to know if a character is a letter) p y t h o n

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • Python Write a function index_last(elem, seq) that takes as inputs an element elem and a sequence...

    Python Write a function index_last(elem, seq) that takes as inputs an element elem and a sequence seq, and that uses recursion (i.e., that calls itself recursively) to find and return the index of the last occurrence of elem in seq. The sequence seq can be either a list or a string. If seq is a string, elem will be a single-character string; if seq is a list, elem can be any value. Don’t forget that the index of the first...

  • Write two functions: Main should call the first function. The first function should use getline to...

    Write two functions: Main should call the first function. The first function should use getline to read a line of data: getline(cin,variableToHoldLineOfData); It should then call the second function, sending the line of data it just read. The second function should determine the number of CAPITAL letters, the number of lowercase letters, and the number of everything else (that's not capital or lower case). Count the length of the string and then use a for loop to go through each...

  • Hi, I have Python programming problem as follow: Thank you. Best Regards. Write a function solution...

    Hi, I have Python programming problem as follow: Thank you. Best Regards. Write a function solution that, given two integers A and B, returns a string containing exactly A letters 'a' and exactly B letters 'b' with no three consecutive letters being the same in other words, neither "aaa" nor "bbb" may occur in the returned string). Examples: 1. Given A = 5 and B = 3, your function may return "aabaabab". Note that "abaabbaa" would also be a correct...

  • I need help writing a very basic code in python. The code should not be very...

    I need help writing a very basic code in python. The code should not be very complicated. The code must have the following things in it: 1. create a function that takes a state and length of characters and makes a license plate and use the function to make a plate based on user input (use the def function and random.choice as well as import random) 2. the license plate should be a mix of random letters and numbers based...

  • Write a function count_vowels(s) that takes a string as an argument and returns the number of...

    Write a function count_vowels(s) that takes a string as an argument and returns the number of vowels ('a', 'e', 'i' 'o', 'u') in the string. Should you use a for or while loop? (Implement this as a function, not as a class with a method.) Be sure to include unittest test cases to demonstrate that your code works properly, e.g Part 2: last_occurance(target, sequence) Write a function last_occurance(target, sequence) that takes two arguments: 1. target: A target item to find...

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
ADVERTISEMENT