Question

Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...

Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it.

Q2)

Write a Python function called lines_of_code that takes a Path object as a parameter, which is expected to be the path to a Python script. It returns how many lines of Python code are contained within the script. Note that not all lines of text are considered to be "lines of code." We'll want to skip a few things, so we aren't counting lines that lack meaning; do not count lines that have these characteristics:

  • A line containing no text
  • A line containing nothing but spaces and/or tabs
  • A line whose only non-space or non-tab characters are part of a comment

(Note that when I refer to "comments" here, I'm referring only to comments, i.e., the portions of a line that begin with a # character. Some people use string literals as comments — most notably, they use multiline string literals, because Python lacks multiline comments and a string literal "floating" in one's code turns out to be legal, though it's not a comment, because it still gets evaluated at run-time — but these aren't comments, so you would want to count them.)

The function returns an integer that indicates how many lines of code are in the Python script.

If the file cannot be opened successfully, or if the file contains something other than text, the function should raise an exception, though it's not important what kind of exception is raised — any exception will do, and it's fine if you raise different kinds of exceptions in different scenarios. However, the file is required to be closed in any case in which it had been opened.

Homework Answers

Answer #1

ANSWER 1

def partial_print(s):
    s = '^'+'^'.join([s[i] for i in range(len(s)) if i%2==0])+"^\n"
    print (s)

ANSWER 2

import re
from pathlib import Path
def lines_of_code(p):
    cnt = 0
    try:
       f = p.open()
    except Exception as e:
        print(str(e))
        exit(1)
    line = f.readline()
    while(line != ""):
        if "#" == line[0]:
            line = f.readline()
            continue
        if re.search(r"\w",line):
            cnt+=1
        line = f.readline()
    return cnt
Know the answer?
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
Similar Questions
Write a Python function that takes a filename as a parameter and returns the string 'rows'...
Write a Python function that takes a filename as a parameter and returns the string 'rows' if a file was written row by row, and 'columns' if the file was written column by column. You would call the function with a command like filetype = myfunc(filename).
Write a function named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "i" replaced with the character "n python
Python The capitalize function should receive a word as a parameter in the form of a...
Python The capitalize function should receive a word as a parameter in the form of a string and return one version where the first character is written in capital letters. Other characters must be unchanged. For example ‘hey’ become ‘Hey’. Write the function clearly. Consider that str has the method upper that returns a new string where all characters are capitalized
Write a Python function first_chars that takes one parameter, which is a nested list of strings....
Write a Python function first_chars that takes one parameter, which is a nested list of strings. It returns a string containing the first character of all of the non-empty strings (at whatever depth they occur in the nested list) concatenated together. Here are a couple of examples of how the function should behave when you're done: >>> first_chars(['Boo', 'is', 'happy', 'today']) 'Biht' >>>first_chars(['boo', 'was', ['sleeping', 'deeply'], 'that', 'night', ['as', ['usual']]]) 'bwsdtnau'
Python. create a function that takes a string as a parameter and prints out the following...
Python. create a function that takes a string as a parameter and prints out the following details as the example output below. If the string is "Hello, my name is Starling, Clarice. My, my, my, nice to meet you." The output would be: Unique words: 10 Number of commas: 5 Number of periods: 2 Number of sentences: 2 (HINT: Use the following string methods: split, lower, count, replace, format, and use the following functions: set, len. You may need to...
Write Java code that attempts to call a method named bootUp() which takes a String parameter...
Write Java code that attempts to call a method named bootUp() which takes a String parameter and returns an integer value. The String parameter should be read from a file named "bootConfig.txt" and is the only value in the file. If a BootUpException is thrown, print the Exception object's message. If a DriverException occurs, call the reboot() method and rethrow the original DriverException object. If any other type of Exception is thrown, print a generic error message to the console....
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
Write a function that takes as input an English sentence (a string) and prints the total...
Write a function that takes as input an English sentence (a string) and prints the total number of vowels and the total number of consonants in the sentence. The function returns nothing. Note that the sentence could have special characters such as dots, dashes, and so on. write a python program please.
Q5. Code a function, named CountNumerics, that accepts a parameter of a string , named str1,...
Q5. Code a function, named CountNumerics, that accepts a parameter of a string , named str1, and returns the number of numeric characters (‘0’ .. ‘9’) + in the string. Helpful clues:   The string class has a function called length() that can be accessed by str1.length().   To access an individual character in the string; use string indexing (str1[i]) where I is an integer. Indexing of a string array starts at 0.
3. Implement the function read_matrix_file that consumes a file name (string) as its parameter and returns...
3. Implement the function read_matrix_file that consumes a file name (string) as its parameter and returns a nested list of ints (4x4 matrix) associated with the scores aligning G, A, T, and C. a. The nested list for the provided alignment_matrix text file would be: [[10,5,3,-5],[5,9,6,7], [3,6,12,8],[-5,7,8,10]]. Python language, txt file, sorry
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT