I'm a mostly self-taught programmer, and recently applied for a computation-heavy internship.
As part of the selection process, I was sent, from some recruiter in a distant city, a programming challenge to complete in Python (I used Python 3).
The program accepts a couple of positional arguments, and writes its results to a file.
I need to submit "source code and exact compiler arguments used to compile." I'm looking for enlightenment on what the last bit means.
From Python3 idle, if programFile contains only function definitions, I can do
>>> import programFile
>>> function(arg1,arg2)
to get the output. Or if I add an executable statement to the programFile, I can do python programFile.py from the command line to get the output. I don't know if one of these is the compiler argument, or if I need something else which would only compile the code.
Any guidance would be appreciated.
The recruiter is using generic terminology that doesn't apply to Python. No compiler is used, so no compiler arguments are required either.
Send the source file and minimum Python version requirement.
Python is an interpreted language you don't need to compile the python file(.py) in order to execute it.
I think the recruiter is using the same message for every language in their program.
I would only send the source code, document it on how to use it and you will be good.
Related
Why is code like this apparently legal (does not give a syntax error when running) in Python 3?
wut:2+2=5
I've tried to find out what this type of syntax could mean and I could not find it. It seems like a key-value pair, but can you just have one lying around? And "2+2=5" is not a valid... anything, is it?
They are type hints in python. Basically, you hint the type of the object(s) you're using. Type-hints are for maintainability and don't get interpreted by Python. So, you might say that a variable has type float whereas python will internally interpret as int only. Eg -
my_var:float = 4
print(type(my_var))
OUTPUT :
<class 'int'>
In your case, it doesn't make much sense. But to ease maintainability when your projects grow large, it is helpful. These can be considered as the next step from # type comments. Eg-
my_var = 4.2 # type: float
my_var: float = 4.2
The above two codes can be considered to be similar in their functionalities.
You can read more about it here - https://docs.python.org/3/library/typing.html
More on this - What are type hints in Python 3.5?
I want to implement a argument type checker. I've read several times about Python and Duck typing, but I'm tired of hunting bugs when I could easily enforce the type of inputs for my functions.
My plan is to implement a type checker that right after the function definition, receives the inputs and does its assertion job.
Something like this
import sanity_check
fun1(a,b):
sanity_check.fun1(a,b)
<do something>
fun2(a,b):
sanity_check.fun2(a,b)
<do something>
It is not my intention for this type checker to clearly estate what is checking (that is left for the comments on the functions), but just to enforce types.
My idea would be that after implementation, I can erase this sanity check module by just automatically erase all lines with the "sanity_check" word. So, it is not intended permanent use, just during implementation.
Onto my question. I do not want to be constantly erasing and copying back these lines whenever I want to test for real the code, since given the nature of the codes I'm implementing, I know the function call overhead will make significant delays on my codes.
Is it there a way to ignore all the members of this "sanity_check" module?
Setting all the members to None could be a way, but I do not know how to do this.
It sounds like you want a combination of type annotations with a static type checker like mypy, plus some assert statements:
Assert statements are a convenient way to insert debugging assertions into a program [..] The current code generator emits no code for an assert statement when optimization is requested at compile time.
You can use this to make runtime checks in debug mode and choose to run your code using the -O flag to omit assert statements and get maximum performance.
Static type hints can catch other types of problems without incurring (significant) runtime overhead; see https://mypy.readthedocs.io/en/stable/getting_started.html#function-signatures-and-dynamic-vs-static-typing.
Example:
def foo(bar: list):
assert len(bar) >= 3, 'List must be at least 3 long, got %d' % len(bar)
...
mypy will help you find bugs where you're not even passing a list into foo, while the assert statement will warn you at runtime if the list is too short, and the check can be omitted if you run the code via python -O foo.py.
I am a python developer making the shift to Golang, so I'm sorry for the noob question. I am responsible for taking some Haskell code, for which we have python bindings, and making it callable from Go. I have a shared object file, _foo.so, that I want to somehow import into Go and call a la:
import (
f "_foo.so"
)
func DoBar() {
return f.Bar()
}
Is this possible? I don't even have the first idea of where to begin, but I'm hoping that pseudo code gets the idea across.
As already mentioned in the comments, you need to go through C.
Good news: the python binding you have goes through C already. It means that haskell code exposes all the necessary API as C functions, you just need to find out how the API looks like and call it using cgo. You probably don't need to know anything about haskell.
Assuming you have access to the source code, you should look for *.c and *.h files (often located in a cbits folder). If you don't know C, then ask your teammates to help.
If you don't have access to the code, then you may try to guess the C API using the python binding. Though it'll be quite hard.
I was intrigued by this question, so I implemented it.
See this repo: https://github.com/rusq/gohaskell/
Go program calls fibonacci Haskell function.
Having an Octave script (in the sense of dynamic languages here) move.m defining function move(direction), it can be invoked from another script (alternatively from the command line) in different ways: move left, move('left') or move(left). While the first two will instantiate direction with the string 'left', the last one will consider left as a variable.
The question is about the formal principle in language definition behind this. I understand that in the first mode, the script is invoked as a command, considering that the rest of the command line is just data, not variables (pretty much as in a Linux prompt); while in the last two it is called as a function, interpreting what follows (between parenthesis) as either data or variables. If this is a general design criteria among scripting languages, what is the principle behind it?
To answer your question, yes, this is by design, and it's syntactic sugar offered by matlab (and hence octave) for running certain functions that expect only string arguments. Here is the relevant section in the matlab manual: https://uk.mathworks.com/help/matlab/matlab_prog/command-vs-function-syntax.html
I should clarify some misconceptions though. First, it's not "data" vs "variables". Any argument supplied in command syntax is simply interpreted as a string. So these two are equivalent:
fprintf("1")
fprintf 1
I.e., in fprintf 1, the 1 is not numeric data. It's a string.
Secondly, not all m files are "scripts". You calling your m file a script caused me some confusion. Your particular file contains a function definition and nothing else, so it's a function, 100%.
The reason this is important here, is that all functions can be called either via functional syntax or command syntax (as long as it makes sense in terms of the expected arguments being strings), whereas scripts take no arguments, so there is no functional / command syntax at play, and if you were passing 'arguments' to a script you're doing something wrong.
I understand that in the first mode, the script is invoked as a command [...]
As far as Octave goes, you are better off forgetting about that distinction. I'm not sure if a "command" ever existed but it certainly does not exist now. The command syntax is just syntactic sugar in Octave. Makes it simpler for interactive plot adjustment since it's functions arguments mainly take strings.
Computers can only understand machine language. Then how come interepreters execute a program directly without translating it into machine language? For example:
<?php
echo "Hello, World!" ;
It's a simple Hello World program written in PHP. How does it execute in machine while the machine has no idea what echo is? How does it output what's expected, in this case, the string Hello, World!?
Many interpreters, including the official PHP interpreter, actually translate the code to a byte code format before executing it for performance (and I suppose flexibility) reasons, but at its simplest, an interpreter simply goes through the code and performs the corresponding action for each statement. An extremely simple interpreter for a PHP-like language might look like this for example:
def execute_program(prog)
for statement in prog.toplevel_statements:
execute_statement(statement)
def execute_statement(statement):
if statement is an echo statement:
print( evaluate_expression(statement.argument) )
else if statement is a for loop:
execute_statement(statement.init)
while evaluate_expression(statement.condition).is_truthy():
for inner_statement in statement.body:
execute_statement(inner_statement)
execute_statement(statement.increment)
else if ...
Note that a big if-else-if statement is not actually the cleanest way to go through an AST and a real interpreter would also need to keep track of scopes and a call stack to implement function calls and returns.
But at its most basic, this is what it boils down to: "If we see this kind of statement, perform this kind of action etc.".
Except for being much more complex, it isn't really any different from writing a program that responds to user commands where the user could for example type "rectangle" and then you draw a rectangle. Here the CPU also doesn't understand what "rectangle" means, but your code contains something like if user_input == rectangle: [code to draw a rectangle] and that's all you need.
Strictly speaking, the interpreter is being executed and the code that the interpreter is interpreting just determines what actions the interpreter takes. (If it was just compiled to machine code, what would you need the interpreter for?).
For example, I built an automation framework awhile back where we captured reflection metadata on what was occurring at runtime during QA tests. We serialized that metadata to JSON. The JSON was never compiled to anything - it just told the automation engine what methods to call and what parameters to pass. No machine code involved. It wouldn't be exactly correct to say that we were "executing" the JSON - we were executing the automation engine, which was then following the "directions" found in the JSON, but it was certainly interpreting the JSON.