Anonymous avatar Anonymous committed 48fc0b8

Added join() and joinfields() functions.
Fixed center().
Rewrote ljust() and rjust().

Comments (0)

Files changed (2)

 	res.append(s[i:])
 	return res
 
+# Join words with spaces between them
+def join(words):
+	res = ''
+	for w in words:
+		res = res + (' ' + w)
+	return res[1:]
+
+# Join fields with separator
+def joinfields(words, sep):
+	res = ''
+	for w in words:
+		res = res + (sep + w)
+	return res[len(sep):]
+
 # Find substring
 index_error = 'substring not found in string.index'
 def index(s, sub):
 
 # Left-justify a string
 def ljust(s, width):
-	n = len(s)
-	if n >= width: return s
-	return s + ' '*(width-n)
+	n = width - len(s)
+	if n <= 0: return s
+	return s + ' '*n
 
 # Right-justify a string
 def rjust(s, width):
-	n = len(s)
-	if n >= width: return s
-	return ' '*(width-n) + s
+	n = width - len(s)
+	if n <= 0: return s
+	return ' '*n + s
 
 # Center a string
 def center(s, width):
-	n = len(s)
-	if n >= width: return s
-	return ' '*((width-n)/2) +  s + ' '*(width -(width-n)/2)
+	n = width - len(s)
+	if n <= 0: return s
+	half = n/2
+	if n%2 and width%2:
+		# This ensures that center(center(s, i), j) = center(s, j)
+		half = half+1
+	return ' '*half +  s + ' '*(n-half)
 
 # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
 # Decadent feature: the argument may be a string or a number
 	res.append(s[i:])
 	return res
 
+# Join words with spaces between them
+def join(words):
+	res = ''
+	for w in words:
+		res = res + (' ' + w)
+	return res[1:]
+
+# Join fields with separator
+def joinfields(words, sep):
+	res = ''
+	for w in words:
+		res = res + (sep + w)
+	return res[len(sep):]
+
 # Find substring
 index_error = 'substring not found in string.index'
 def index(s, sub):
 
 # Left-justify a string
 def ljust(s, width):
-	n = len(s)
-	if n >= width: return s
-	return s + ' '*(width-n)
+	n = width - len(s)
+	if n <= 0: return s
+	return s + ' '*n
 
 # Right-justify a string
 def rjust(s, width):
-	n = len(s)
-	if n >= width: return s
-	return ' '*(width-n) + s
+	n = width - len(s)
+	if n <= 0: return s
+	return ' '*n + s
 
 # Center a string
 def center(s, width):
-	n = len(s)
-	if n >= width: return s
-	return ' '*((width-n)/2) +  s + ' '*(width -(width-n)/2)
+	n = width - len(s)
+	if n <= 0: return s
+	half = n/2
+	if n%2 and width%2:
+		# This ensures that center(center(s, i), j) = center(s, j)
+		half = half+1
+	return ' '*half +  s + ' '*(n-half)
 
 # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
 # Decadent feature: the argument may be a string or a number
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.