Question

Java Python Warmup-2 > string_times prev next | chance Given a string and a non-negative int n, return a larger string that iJava Python String-1 > extra_end prev next chance Given a string, return a new string made of 3 copies of the last 2 chars ofJava Python String-1 > make_tags prev next chance The web is built with HTML strings like <i>Yay</i> which draws Yay as itaJava Python String-1 > combo_string prev next chance Given 2 strings, a and b, return a string of the form short+long+short,Java Python String-2 > count_code prev next chance Return the number of times that the string code appears anywhere in theJava Python String-2 > xyz_there prev next | chance Return True if the given string contains an appearance of xyz where theComplete the following codingbat exercises: 1. string times 2 2. extra end 3. make tags 4. combo string 2 5. count_code_ (YouThis CSIS 9 Python.

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

def string_times (str,n): return n str def extra end(str): endTwo = str[-2] + str[-1] return 3 endTwo def make_tags(tag, word

Warmup-2 > string_times

A better solution for printing a string for n number of times is by taking advantage of built in mechanisms in python.

Eg - In python if str='a' and str1 = 4*str then str1 would store the vale 'aaaa' we use this mechanism for solving the problem in a cleaner way.

Code:

def string_times(str,n):
    return n*str

String-1 > extra_end

To index strings from the end we use negative indexes in python i.e. in str='tank', str[-1] would give us the last character in the string 'k', similaly str[-2] would be 'n'.

We create a string by taking the last two characters and using the string_times mechanism return a string with 3 copies.

Code:

def extra_end(str):
   endTwo = str[-2] + str[-1]
   return 3*endTwo

String-1 > make_tags

We create the two tags openTag and closeTag by using string concatenation. And then return a string word by concatenating the string openTag at the front and closeTag at the end.

Code:

def make_tags(tag, word):
   openTag = '<'+tag+'>'
   closeTag = '</'+tag+'>'
   return (openTag+word+closeTag)

String-1 > combo_string

In this problem we first check with an if statement if the length of string a is more and enclose it within string b(using string concatenation like in previous problems) if the condition is true and return the string.

Else if the length of string a is less than b then we enclose the string b with a and return the value.

Code:
def combo_string(a,b):       
   if len(a) > len(b):
       return b+a+b

   elif len(a) < len(b):
       return a+b+a

Add a comment
Know the answer?
Add Answer to:
This CSIS 9 Python. Java Python Warmup-2 > string_times prev next | chance Given a string...
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
  • Given two int values, return their sum. Unless the two values are the same, then return...

    Given two int values, return their sum. Unless the two values are the same, then return double their sum sum_double (1, 2) 3 sum_double (3, 2) 5 sum_double (2, 2)8 def sum_double(a, b) Python Warmup-2 > front_times prev next chance Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Retun n copies of the front Expected...

  • Styles 6 2. Modify the "LinkedStackOfStrings java" program given in the textbook (program 4.3.2) by adding...

    Styles 6 2. Modify the "LinkedStackOfStrings java" program given in the textbook (program 4.3.2) by adding a method "find0 that takes a string as an argument. It should return true if the string argument is in the linked stack and false otherwise. [Mo6.1] The main method should also be modified so that all the strings (other than the search string) inputted by the user are stored on the stack before calling the find0 method. Sample runs would be as follows....

  • I need help with these java codingbat questions please: Create a method that creates a user...

    I need help with these java codingbat questions please: Create a method that creates a user id, where the user id is made up of the first letter of the first name and the entire last name. e.g. john smith ->jsmith getUserld("bill", "gates")-"bgates" getUserId("steve", "jobs")"sjobs" getUserId("larry", "page") -"lpage" Go Save, Compile, Run (ctrl-enter) public String getUserId( String firstName, String lastName) Given a number n, create an array with elements starting from 1 to n. e.g. 5 > getNumArray(1) [1] getNumArray...

  • Has to be in Java programming language Java Python mirrorLength We'll say that a "mirror" section...

    Has to be in Java programming language Java Python mirrorLength We'll say that a "mirror" section in an array is a group of contiguous elements such that somewhere in the array, the same group appears in reverse order. For example, the largest mirror section in {1, 2, 3, 8, 9, 3, 2, 1} is length 3 (the {1, 2, 3} part). Return the size of the largest mirror section found in the given array. 3 mirrorLength([1, 2, 3, 8, 9,...

  • Please write a Java program: Given an array of positive integers, return a count of the...

    Please write a Java program: Given an array of positive integers, return a count of the number of even integers. countEvens([2, 3, 5])-1 countEvens([4, 20]) - 2 countEvens([3, 7, 1, 11]) 0 Go Save, Compile, Run (ctrl-enter) int countEvens (int[] nums) {

  • Here's what I need you to do. Thank you to whoever is helping me in solving...

    Here's what I need you to do. Thank you to whoever is helping me in solving this problem. Please go on CodingBat.com and look for Logic-2, close_far. Once its found, type the code in their and click on "go" to compile the code. Once again, thank you. I'm going to comment down below to let you know if the code works or not. Logic-2 close far prev I next chance Given three ints, a b c, return True if one...

  • Page 3 of 7 (Python) For each substring in the input string t that matches the...

    Page 3 of 7 (Python) For each substring in the input string t that matches the regular expression r, the method call re. aub (r, o, t) substitutes the matching substring with the string a. Wwhat is the output? import re print (re.aub (r"l+\d+ " , "$, "be+345jk3726-45+9xyz")) a. "be$jk3726-455xyz" c. "bejkxyz" 9. b. "be$jks-$$xyz" d. $$$" A object a- A (2) 10. (Python) What is the output? # Source code file: A.py class A init (self, the x): self.x-...

  • \ Please use JAVA to solve this problem. Thanks! Given a non-negative integer, return true if...

    \ Please use JAVA to solve this problem. Thanks! Given a non-negative integer, return true if most of its digits are odd (more digits are odd than are even). An odd number is any number ending with 1, 3, 5, 7, or 9 mostlyOdd(47) false mostlyOdd(79) → true mostlyOdd(286) - false Go Save, Compile, Run (ctrl-enter) boolean mostlyOdd(int num)

  • I am using Python 3.5.2 2. Create a function called is_interesting that, given a string, returns...

    I am using Python 3.5.2 2. Create a function called is_interesting that, given a string, returns the Boolean value True if the number of vowels (a e i o u) in the string is a prime number (assume that the strings are always passed as lower-case letters to simplify). The function header should be as follows: def is_interesting(a_string) For example is_interesting(“yien”) returns True, is_interesting(“wang”) returns False

  • Convert Python to Java def read_fsm(filename): fh = open('fsm.txt','r') contents = fh.readlines() sigma...

    Convert Python to Java def read_fsm(filename): fh = open('fsm.txt','r') contents = fh.readlines() sigma = list(contents[0].rstrip().split(' ')) table = {} n = int(contents[1]) for i in range(n): table[i] = {} final = list(map(int,contents[2].rstrip().split(' ')))    for line in contents[3:]: fro,ip,to = line.split(' ') fro = int(fro) to = int(to) table[fro][ip] = to print(table) fh.close() return table,final    def runString(table,final,string): current = 0 for i in string: current = table[current][i] if current in final: print(string,'--> ACCEPT') else: print(string,'--> REJECT')    def readInput(table,final): fh = open('Strings.txt','r')...

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
Active Questions
ADVERTISEMENT