Skip to content

Instantly share code, notes, and snippets.

View luisenriquecorona's full-sized avatar
😎
I may be slow to respond.

TextKi JS luisenriquecorona

😎
I may be slow to respond.
View GitHub Profile
@luisenriquecorona
luisenriquecorona / CheckSum.java
Created April 17, 2019 23:48
CheckSum one file, given an open BufferedReader.
/** 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);
}
@luisenriquecorona
luisenriquecorona / SortingTable.js
Created April 18, 2019 00:13
Sort the rows in first of the specified table according to the value of nth cell within each row.
// 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
@luisenriquecorona
luisenriquecorona / outerHTML.js
Created April 18, 2019 00:23
Implement the outerHTML property for browsers that don't support it
// 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
@luisenriquecorona
luisenriquecorona / innerHTML.js
Created April 18, 2019 00:35
Implementing insertAdjacentHTML() using innerHTML
// 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);},
@luisenriquecorona
luisenriquecorona / For.py
Created April 18, 2019 15:50
Get for test Python
>>> for x in 'spam':
... print(x) # Press Enter twice before a new statement ... print('done')
File "<stdin>", line 3 print('done')
^
SyntaxError: invalid syntax
@luisenriquecorona
luisenriquecorona / bytearray.py
Created April 18, 2019 17:28
Translate to normal string
>>> 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
@luisenriquecorona
luisenriquecorona / Type.py
Created April 18, 2019 17:33
How to Break Your Code’s Flexibility
# 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
@luisenriquecorona
luisenriquecorona / Helloword.py
Created April 18, 2019 17:34
Test text Python
>>> 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
@luisenriquecorona
luisenriquecorona / Classes.py
Created April 18, 2019 17:38
User-Defined Classes
>>> 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()
@luisenriquecorona
luisenriquecorona / Names Types.py
Created April 18, 2019 17:42
Set comprehensions in Python 3.X and 2.7
>>> 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?