Question

edef extract_num(s, begin, end): Given string s, and begin is the index of the first of one or more digits, and end is th
29 def. parse line(s): Given a string s, parse the ints out of it and return them as a list of int values. >>> parse_line( 1
a. extract_num(s, begin, end) The trickiest part of this problem is this: we have found one or more digits in a string, and w
b. parse_line(s) Given a string, such as a line from a data file, extract all the numbers as described above from the line an
Each number is represented by some text starting with a digit, following these rules: 1. The numbers are non-negative integer
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the code attached for above question in Python:

#function to extract number from the string.
def extract(s,begin,end):
numbers = [] #empty number list
temp = [] #empty temporary list to store number
for i in range(begin,end): #iterating over string from begin to end
if s[i].isdigit(): #conditional statement to check if element is numeric or not
temp.append(s[i]) #if numeric then adding into temporary list.
else: #if not numeric then
if s[i]=='$': #if element is special character '$' then
#first reversing temp list by temp[::-1])
#then,typecasting list element as string.
#joining list element to form a string
#appended string to numbers list
numbers.append(''.join(map(str,temp[::-1])))
  
elif s[i]!='^':
#if element is '^' we omit it else ,
#typecasting list element as string.
#joining list element to form a string
#appended string to numbers list
numbers.append(''.join(map(str,temp)))
temp = [] #emptying the temp list
#removing all empty element from the numbers list
str_list = list(filter(None, numbers))
#converting every element from string to integer in str_list
for i in range(len(str_list)):
str_list[i] = int(str_list[i])
#returns list
return str_list

#funtion parse_line that parses each line of text file and calls extract to extract numbers
def parse_line(s):
number = extract(s,0,len(s)) #calls extract function
#displays numbers list on console.
print(number)
  
file_name = 'file.txt' #file name

#using with operator to handle file
with open(file_name) as f:
line = f.readline().strip() #reading line and removing any empty spaces at the end.
#reading file's each line.
while(line):
parse_line(line) #calling parse_file function.
line = f.readline().strip()
Refer to the screenshot attached below to better understand the indentation and code:
1 #function to extract number from the string. 2 def extract(s, begin, end): numbers = [] #empty number List temp = [] #empty

31 #funtion parse_line that parses each Line of text file and calls extract to extract numbers 32 def parse_line(s): number =
Input File:

800!)176^b006$(46$*#63z*16$*06$z5^
455^176$b785$46$##63z*16$*4$z5^
781!)16$*06$z5^(83$*#45z*16$*06$z45^
369!)(36$*#63z*16$*06$z5^176^b0005$
%%##%%##)()&@^#!#

Screenshot of Input File:
file - Notepad - O X File Edit Format View Help 800!) 176^b006$ (46$*#63z*16$*06$25^ 4554176$b785$46$##63z*16$*4$25^ 781!) 16

Output:

[888,68,64,63,51,68] [671,587,64,63, 61, 4] [781, 61, 8, 8,45, 61.62] [39,63,3,61,68,5eee]

If my answer helps then please upvote ,it means alot
for further queries comment below.

Thank You

Add a comment
Know the answer?
Add Answer to:
edef extract_num(s, begin, end): Given string s, and "begin" is the index of the first of...
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
  • In the first task, you will write a Java program that accepts a string of the...

    In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

  • Here is the UML for the class Contact -fullName : string -phoneNum: string -emailAddress : string...

    Here is the UML for the class Contact -fullName : string -phoneNum: string -emailAddress : string +Contact()                     //default constructor sets all attribute strings to “unknown”; no other constructor +setName(string name) : void +setPhone(string pn) : void +setEmail(string em) : void +getName() : string +getPhone() : string +getEmail() : string +printContact() : void         //print out the name, phone numbers and email address Create New Project Name your project: CIS247_Lab7_YourLastName. For this exercise, you may use a header file or one file...

  • c++ pleased Assignment 6a (15 points] - Character and String related processing... Listed below are two...

    c++ pleased Assignment 6a (15 points] - Character and String related processing... Listed below are two interesting programming challenges that will require a bit of character and/or string related processing with functions. (Carefully read the specifications for each programming challenge and select ONE.) (1) Sum of Digits in a String Write a program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program...

  • DO NOT USE ANY EXTERNAL LIBRARIES BESIDES BUILT IN JAVA LIBRARIES! KEEP IT SIMPLE!!! Provided Code:...

    DO NOT USE ANY EXTERNAL LIBRARIES BESIDES BUILT IN JAVA LIBRARIES! KEEP IT SIMPLE!!! Provided Code: import java.util.Scanner; public class OddAndEven{ /* PART 1: Create a nonstatic method that takes in an int number quantity (n) and returns a returns a String of numbers from 0 to n (inclusive) as the example above demonstrates. Call this quantityToString.    In this method you should check that n is between 0(inclusive) and 100(inclusive). If n is outside these boundaries return and empty...

  • Please help me. package a1; public class Methods {    /*    * Instructions for students:...

    Please help me. package a1; public class Methods {    /*    * Instructions for students: Use the main method only to make calls to the    * other methods and to print some testing results. The correct operation of    * your methods should not depend in any way on the code in main.    *    * Do not do any printing within the method bodies, except the main method.    *    * Leave your testing code...

  • Your task is to develop a large hexadecimal integer calculator that works with hexadecimal integers of...

    Your task is to develop a large hexadecimal integer calculator that works with hexadecimal integers of up to 100 digits (plus a sign). The calculator has a simple user interface, and 10 \variables" (n0, n1, ..., n9) into which hexadecimal integers can be stored. For example a session with your calculator might look like: mac: ./assmt1 > n0=0x2147483647 > n0+0x3 > n0? 0x214748364A > n1=0x1000000000000000000 > n1+n0 > n1? 0x100000000214748364A > n0? 0x214748364A > exit mac: Note: \mac: " is...

  • Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong...

    Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong here is what i was told that was needed. Ill provide my code at the very end. Here is what is missing : Here is my code: public class GSL { private String arr[]; private int size; public GSL() {     arr = new String[10];     size = 0; } public int size() {     return size; } public void add(String value) {    ...

  • In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first'...

    In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and 'last' is the last index void writeArrayBackward(const char anArray[], int first, int last) { int i = 0; for (i = last; i >= first; i--) { std::cout << anArray[i]; } std::cout << std::endl; } // test driver int main() { const char *s = "abc123"; writeArrayBackward(s, 0, strlen(s) - 1); } // Using the...

  • ; original codes provided as following .ORIG x3000        ; begin at x3000 ; input two numbers...

    ; original codes provided as following .ORIG x3000        ; begin at x3000 ; input two numbers IN                               ; input an integer character (ascii) {TRAP 23} LD R3, HEXN30 ; subtract x30 to get integer ADD R0, R0, R3 ADD R1, R0, x0 ; move the first integer to register 1 IN                               ; input another integer {TRAP 23} ADD R0, R0, R3 ; convert it to an integer ; add the numbers ADD R2, R0, R1 ; add the two integers...

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