This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** CheckSum one file, given an open BufferedReader. */ | |
public int process(BufferedReader is) { | |
int sum = 0; | |
try { | |
String inputLine; | |
while ((inputLine = is.readLine( )) != null) { | |
int i; | |
for (i=0; i<inputLine.length( ); i++) { | |
sum += inputLine.charAt(i); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sort the rows in first <tbody> of the specified table according to | |
// the value of nth cell within each row. Use the comparator function | |
// if one is specified. Otherwise, compare the values alphabetically. | |
function sortrows(table, n, comparator) { | |
var tbody = table.tBodies[0]; // First <tbody>; may be implicitly created | |
var rows = tbody.getElementsByTagName("tr"); // All rows in the tbody | |
rows = Array.prototype.slice.call(rows,0); // Snapshot in a true array | |
// Now sort the rows based on the text in the nth <td> element | |
rows.sort(function(row1,row2) { | |
var cell1 = row1.getElementsByTagName("td")[n]; // Get nth cell |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Implement the outerHTML property for browsers that don't support it. | |
// Assumes that the browser does support innerHTML, has an extensible | |
// Element.prototype, and allows getters and setters to be defined. | |
(function() { | |
// If we already have outerHTML return without doing anything | |
if (document.createElement("div").outerHTML) return; | |
// Return the outer HTML of the element referred to by this | |
function outerHTMLGetter() { | |
var container = document.createElement("div"); // Dummy element |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This module defines Element.insertAdjacentHTML for browsers that don't | |
// support it, and also defines portable HTML insertion functions that have | |
// more logical names than insertAdjacentHTML: | |
// Insert.before(), Insert.after(), Insert.atStart(), Insert.atEnd() | |
var Insert = (function() { | |
// If elements have a native insertAdjacentHTML, use it in four HTML | |
// insertion functions with more sensible names. | |
if (document.createElement("div").insertAdjacentHTML) { | |
return { | |
before: function(e,h) {e.insertAdjacentHTML("beforebegin",h);}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> for x in 'spam': | |
... print(x) # Press Enter twice before a new statement ... print('done') | |
File "<stdin>", line 3 print('done') | |
^ | |
SyntaxError: invalid syntax |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> S = 'shrubbery' | |
>>> L = list(S) | |
>>> L | |
['s', 'h', 'r', 'u', 'b', 'b', 'e', 'r', 'y'] >>> L[1] = 'c' | |
>>> ''.join(L) 'scrubbery' | |
>>> B = bytearray(b'spam') >>> B.extend(b'eggs') | |
>>> B bytearray(b'spameggs') | |
>>> B.decode() 'spameggs' | |
# Expand to a list: [...] | |
# Change it in place |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In Python 2.X: | |
>>> type(L) | |
<type 'list'> | |
>>> type(type(L)) <type 'type'> | |
# In Python 3.X: | |
>>> type(L) <class 'list'> >>> type(type(L)) <class 'type'> | |
# Types: type of L is list type object # Even types are objects | |
# 3.X: types are classes, and vice versa # See Chapter 32 for more on class types |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> f = open('data.txt') >>> text = f.read() | |
>>> text 'Hello\nworld\n' | |
>>> print(text) Hello | |
world | |
>>> text.split() ['Hello', 'world'] | |
# 'r' (read) is the default processing mode # Read entire file into a string | |
# print interprets control characters | |
# File content is always a string |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> class Worker: | |
def __init__(self, name, pay): | |
self.name = name | |
self.pay = pay def lastName(self): | |
return self.name.split()[-1] def giveRaise(self, percent): | |
self.pay *= (1.0 + percent) | |
# Initialize when created # self is the new object | |
# Split string on blanks | |
# Update pay in place | |
>>> bob = Worker('Bob Smith', 50000) >>> sue = Worker('Sue Jones', 60000) >>> bob.lastName() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> engineers = {'bob', 'sue', 'ann', 'vic'} >>> managers = {'tom', 'sue'} | |
>>> 'bob' in engineers True | |
>>> engineers & managers {'sue'} | |
>>> engineers | managers | |
{'bob', 'tom', 'sue', 'vic', 'ann'} | |
>>> engineers - managers {'vic', 'ann', 'bob'} | |
>>> managers - engineers {'tom'} | |
>>> engineers > managers False | |
# Is bob an engineer? | |
# Who is both engineer and manager? |