Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7790309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:32:33+00:00 2026-06-01T21:32:33+00:00

I have an assignment where I’m asked to modify some code. The original function

  • 0

I have an assignment where I’m asked to modify some code. The original function is this:

def selectivelyCopy(inputFile,outputFile,predicate):
  linesCopied = 0
  for line in inputFile:
    if predicate(line):#test the line with the predicate
        outputFile.write(line)
        linesCopied+=1
inputFile.close()
return linesCopied

Now I am suppose to add the parameter transform, a function that takes in as its parameter a string, and returns a string according to the transformation specified by the user. If transform is omitted from the function call, lines from the input file are written unchanged.

Here is what I have so far:

def selectivelyCopy2(inputFile,outputFile,predicate, transform):
    def transform(x = lambda x: x):
        return(x) 

    linesCopied = 0
    for line in inputFile:
        if predicate(line): #test the line with the predicate
            outputFile.write(line)
            linesCopied+=1
    inputFile.close()
    return linesCopied

I’m not sure where to proceed from here. I think I’m suppose to read the input file line, but write the transformed line…or something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:32:35+00:00Added an answer on June 1, 2026 at 9:32 pm

    Now I am suppose to add the parameter transform, a function that takes in as its parameter a string, and returns a string according to the transformation specified by the user. If transform is omitted from the function call, lines from the input file are written unchanged.

    That sounds to me very simple: you are supposed to accept a function argument called transform, and if it is supplied, you call it. If it is not supplied, you either don’t call it, or else you call a trivial function that returns its input unchanged.

    I suggest that you use a default argument of None for transform. Then check to see if transform is None. If it is not, then try to call it, passing the current line, and collecting the output as the new current line. If transform is None then you just write the current line unchanged.

    Alternatively, you could declare this trivial function:

    def nop(x):
        return x
    

    And then specify that the default for argument transform is the function nop. Which is better, to test for None and call nothing, or to have a sensible no-operation default function and always call it? I think this is mostly a matter of personal preference. The test for None avoids the overhead of a function call, so it is probably slightly faster, but it probably isn’t a big deal either way.

    There is no reason to declare a private function named transform, and by doing that you are making it impossible to check what the argument transform is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this assignment due that requires the usage of FLTK. The code is
I have an assignment in which I need to create a function that tells
I have an assignment at university, and I have started developing for this in
I have this assignment to prove that this problem: Finite alphabet £, two strings
So i have a assignment to write some REST client calls to a REST
So I have this assignment : I need help with question 2. I thought
I have an assignment that I'm not quite sure where to start. This is
I have an assignment on optimal binary search trees and some questions came up
I have an assignment said that to create a findString function that accept 2
I have this assignment that I've tried. But when I enter 1 it should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.