LMU | CMSI 698
SCRIPTING LANGUAGES
Practice Questions
  1. Why are Python and Ruby not univerally thought of as scripting languages?
  2. How could you reasonably assert that Smalltalk and ML are scripting languages? Under what conditions would you reasonably assert that they were not?
  3. Write an essay (or article) that distinguishes scripting language from dynamic language. Use and cite scholarly sources.
  4. Write, in bash, Perl, Python, and Ruby
    1. a script that takes in a hex string (which must contain between 1 and 8 digits inclusive) and writes, to standard output, the corresponding IPv4 dotted-decimal representation.
    2. a script that writes, to standard output, an IPv4 dotted decimal representation into its corresponding hex value

    Make sure the scripts handle all errors well.

  5. Write functions (not full scripts), to convert between 32-bit hexadecimal numerals and dotted-decimal strings for IPv4 addresses in bash, Windows PowerShell, Perl, Python, Ruby, and JavaScript. For languages that support exceptions, throw an exception if the argument cannot be converted.
  6. Write, in any scripting language, a script to display on standard output the name of a random playing card, e.g. "jack of diamonds" or "three of spades".
  7. Write, in any scripting language, a script that expands all tabs to spaces. The script should take an optional argument called tabsize which defaults to 8. Sample invocations:
        tabs_to_spaces -width 4 somefile.txt
        tabs_to_spaces somefile.txt
    
  8. Describe three features of zsh that are not in bash.
  9. Write a bash function that takes in a hostname, a path, and a port (default to 80 if not present), and writes, to standard output, the result of an HTTP GET request to that host, path, and port.
  10. How can you make a single-quoted string in bash that contains the single quote character?
  11. Write a bash function called join that takes an array and a separator string and writes to standard output the array values separated by the separator string.
  12. Explain, in English, what this fragment of a bash script does:
    case "$1" in
    0x*)	h=${1#??} ;;
    *)	h=$1 ;;
    esac
    
  13. Give the abstract syntax tree for the following Perl fragment Note that die and split are both list operators, as opposed to unary operators:
        die "\n">>1,~\$$x^split @a,3|x**$;=>%p&&&p;
    
  14. Give the abstract syntax tree for the following Perl fragment:
        sub f {
            print $x[0] unless q/pig =~ m!^r$!/ =~ m!^\((4)+!x;
            push @_, sort reverse keys %$shift, g A 1, 2
        }
    
  15. What can't you do with a Perl package named m, s, or y? Why?
  16. Write a Perl "class" for machine parts that have an identification number (a positive integer divisible by 5), a weight (a positive floating-point number) and a name (which must consist entirely and exclusively of alphabetic characters). Provide a "constructor" that takes in a string consisting of the id, weight, and name, respectively in which Use a regex for validating or parsing the constructor argument.
  17. Write in Perl, a pair of functions, f and g, such that every time you call f, you get back 5 less than the result of the previous call to f or g, and every time you call g, you get back double the absolute value of the result of the last call to f or g. The "initial value" is 0.
  18. Make a Perl module with a function called nextOdd. The first time you call this subroutine you get the value 1. The next time, you get a 3, then 5, then 7, and so on. Show a snippet of code that uses this subroutine from outside the module. Is it possible to make this module hack-proof? In other words, once you compile this module, can you be sure that malicious code can't do something to disrupt the sequence of values resulting from successive calls to this function?
  19. Write Perl subroutines that takes in a file handle and returns a reference to a hash that maps an integer x to the number of lines in that file that contains x characters (not including the newline), up until the point in the file that contains two blank lines. For example, if the file contains
    Blah
    dog dog 123
    Four
    
    Zero
    1234567890*
    
    
    More stuff!
    1234
    
    Then you should return the hash reference {4=>3,11=>2,0=>1} because, before the part in the file with two blank lines, there are three lines with four characters, two lines with 11 characters, and 1 line with zero characters. The lines after the two blank line sequence are not considered, nor are the lines in the two blank line sequence. If there is no place in the file with two blank lines, then all lines will be counted.
  20. Write, in Python, a list comprehension for the list of all non-primes between 1 and 100 (it's okay if this list contains duplicates). Then, using what you just wrote, write a list comprehension for the list of all primes between 1 and 100. (Hint: it will use the "not in" operator.)
  21. For each of the following list comprehensions in Python, write an equivalent expression to produce the list using map (and possibly lambda and reduce). If it is not possible to construct the list with a single call of map, state why it is not possible.
    1. [x for x in range(0,5)]
    2. [x for x in range(0,5) if x % 2 == 0]
    3. [(x,y) for x in a for y in b]
  22. It is sometimes claimed that Python does not have true closures? What is meant by this? Does this claim apply to the most recent version of Python? How does one simulate a closure? Are callables not effective or as elegant as, say, Ruby's blocks?
  23. Produce a table with six rows and six columns, where the rows and columns are labeled with the six JavaScript types, and the content of each cell describes how expressions of the type labeling the row are implicitly coerced to expressions of the type labeling the column (in JavaScript, of course).