r/dailyprogrammer • u/jnazario 2 0 • Jun 06 '16
[2016-06-06] Challenge #270 [Easy] Challenge #270 [Easy] Transpose the input text
Description
Write a program that takes input text from standard input and outputs the text -- transposed.
Roughly explained, the transpose of a matrix
A B C
D E Fis given by
A D
B E
C FRows become columns and columns become rows. See https://en.wikipedia.org/wiki/Transpose.
Formal Inputs & Outputs
Input description
One or more lines of text. Since the transpose is only valid for square matrices, append spaces to the shorter lines until they are of the same length. Characters may be multibyte (UTF-8) characters.
Some
text.Output description
The input text should be treated as a matrix of characters and flipped around the diagonal. I.e., the top right input character becomes the bottom left character of the output. Blank space at the end of output lines should be removed. Tab (\t) may be treated like any other character (don't replace it with spaces).
St
oe
mx
et
.Note that the lower left character is a space in the output, but nothing in the input.
Input
package main
import "fmt"
func main() {
queue := make(chan string, 2)
queue <- "one"
queue <- "twoO"
close(queue)
for elem := range queue {
fmt.Println(elem)
}
}Output
p i f }
a m u
c p n
k o c
a r qqqcf }
g t muuulo
e aeeeor
" iuuus
m f neeeeef
a m ( (lm
i t ):<<qet
n " =--um.
{ e P
m""u:r
aote=i
knw) n
eeo rt
("O al
c " nn
h g(
a ee
n l
qe
s um
t e)
r u
i e
n
g {
,
2
)Credit
This challenge was suggeted by /u/Gommie. Have a good challenge idea? Consider submitting it to /r/dailyprogrammer_ideas .
5
u/jnd-au 0 1 Jun 06 '16
Correction: Hi /u/jnazario I think there are some UTF-8 encoding problems with the challenge text (e.g. <, \u0a66 etc). Shouldn’t the challenge input be:
And the output:
10
u/porphyro Jun 06 '16 edited Jun 06 '16
CJam, 9/30 bytes
Try it online! This version requires additional spaces given in the input.
Try it online! This version does not.
8
u/YOLO_Ma Jun 06 '16
in Clojure
8
u/Aalenox Jun 06 '16
LabVIEW http://i.imgur.com/NW2edDa.png
And the output: http://i.imgur.com/5OwJ6hR.png
7
u/tajjet Jun 07 '16
Python 3. Sentinel input because [Easy]. How do you learn to one-line these?
5
u/Godspiral 3 3 Jun 06 '16
in J,
|: > cutLF wdclippaste ''
4
u/a_Happy_Tiny_Bunny Jun 06 '16 edited Jun 07 '16
Haskell
Not using the standard
transposefunction.EDIT: Alternative solution:
7
u/VilHarvey Jun 06 '16
Python one liner (well, one line of import statements plus one line of actual code):
3
u/jnd-au 0 1 Jun 06 '16
Scala. Attempts to comply with the challenge requirements (e.g. trimming trailing whitespace), which some short matrix solutions don’t.
3
u/The_Jare Jun 06 '16
Haskell
3
u/Hannoii Jun 06 '16
C++11 Very straightforward solution.
3
u/bitx0r Jun 11 '16
AutoHotkey
4
u/Scroph 0 0 Jun 06 '16
Straightforward C++ solution, could be written better but I'm still familiarizing myself with the stdlib :
2
u/revereddesecration Jun 06 '16 edited Jun 06 '16
Lua
If there's a way to get this done in only a handful of lines, I'd like to see it.
Output:
2
u/Blackshell 2 0 Jun 06 '16
It's crappy Javascript time!
This is ES6 Javascript so if it doesn't work in your browser (shame on you, use a newer browser) then you can probably run this Babel'd and Uglify'd version instead:
2
u/weekendblues Jun 06 '16
My solution in Java 8
1
u/moeghoeg Jun 06 '16 edited Jun 06 '16
Python 3:
EDIT: made shorter
1
u/DeLangzameSchildpad Jun 06 '16
Python 3
I did this one in two ways. The first is the proper, more readable way to do it, but the second way is almost a single line function. The actual logic is a single line, but I can't find a way to read all of the input in a single statement unless it was already a string or if I knew how many lines were coming in.
1
u/JakDrako Jun 06 '16
VB.Net
Was initially longer, but the algo LINQifies nicely.
1
u/[deleted] Jun 06 '16 edited Jun 06 '16
Python.
Alternatively, a one-liner:
1
u/ShaharNJIT 0 1 Jun 06 '16
Python 3 I have yet to seriously begin to learn Python so it's not a 1-liner. I tried doing this in Python 2, drowned in encoding problems (with the older input).
+/u/CompileBot Python 3
1
u/blondepianist Jun 06 '16 edited Jun 06 '16
Objective-C
(Edited to make method more concise)
1
u/Stannu Jun 06 '16 edited Jun 06 '16
Here is my amateur JAVA solution. It's my first post here so don't go too hard on me!
Output:
1
u/bearific Jun 06 '16 edited Jun 06 '16
Python 3: ugly oneliner.
Output: https://ideone.com/h27LLZ
1
u/draegtun Jun 06 '16 edited Jun 07 '16
Rebol
Alternative version:
NB. Tested in Rebol 3
1
u/niandra3 Jun 06 '16 edited Jun 06 '16
Basically three lines in Python3. Also, one-liner version for the ridiculous.
+/u/CompileBot Python3
1
u/hutsboR 3 0 Jun 06 '16 edited Jun 06 '16
Elixir: Wrote this directly in the REPL. Pretty dense. My frustration is with the zip function, it's not variadic like it is in other languages. It's arity two, you can't pass it
>2lists. Padding and variadic zipping results in a very succinct solution. With my constraints, this is the shortest possible mess I could muster up. I used a map instead of nested lists because lists aren't designed to be accessed by index in Elixir. Maps implement the access protocol so you can look up a key with this syntaxmap[key]and if it's not in the map it returnsnil, which makes it so I don't have to worry about taking care of an exception or error tuple. It's pretty much abuse but it saves lines.Usage:
1
u/glenbolake 2 0 Jun 06 '16
Python 3 semi-one-liner. If not for the IndexError I handle, I think I could make it into a one-liner.
+/u/CompileBot Python 3
1
u/bbatwork Jun 06 '16
Python 3 solution to transpose any number of lines:
1
u/ultrasu Jun 06 '16 edited Jun 06 '16
Racket:
1
u/Kametrixom Jun 06 '16
Swift
Could be a one-liner if the compiler didn't choke:
1
u/JakDrako Jun 06 '16
C#
Same as earlier VB version, except for a few more Trims and TrimEnds...
1
u/gju_ Jun 06 '16
Go
1
u/InProx_Ichlife Jun 06 '16
Python 3 using zip().
1
u/MichaelPenn Jun 06 '16 edited Jun 07 '16
Python 2.5.1
1
u/AnnieBruce Jun 06 '16
Python. Didn't do any I/O because that's boring.
1
u/voice-of-hermes Jun 07 '16
Cheating, just to be different:
1
u/Daanvdk 1 0 Jun 07 '16 edited Jun 07 '16
In Haskell
+/u/CompileBot Haskell
Input:
1
u/slampropp 1 0 Jun 07 '16
Haskell.
I try to do everything in a single pass of the list(s). No pre-calculating the length of the input lines etc. It came out very clean except for defaultZipWith which is a big fat eye sore. (It's meant to be like zipWith except it zips to the end of the longer of the input lists, filling in missing values in the shorter list with a default value.) Tips on how to clean it up are welcome.
1
u/mbdomecq Jun 08 '16
C++
1
u/tonycarl Jun 08 '16
C# I avoid string trim and allocating multiple strings by reusing a buffer.
Output
.Net Fiddle
1
u/asarsenic Jun 08 '16
Python 2.7 - Not as elegant as a one-liner, but it gets the job done consistently:
1
u/mavrickman9800 Jun 08 '16 edited Jun 08 '16
Java solution. Kinda brute force, but I'm pretty satisfied with it.
1
u/Minolwa Jun 08 '16 edited Jun 08 '16
I've just started getting back into python. I never learned the magic tricks some people here know, so here's an overly verbose python3 solution:
1
u/[deleted] Jun 08 '16
Very inelegant solution in Python 3, any feedback is appreciated.
1
u/[deleted] Jun 08 '16
Python
1
u/[deleted] Jun 09 '16 edited Jun 09 '16
Python 3
Kinda new in Python so, feedbacks are appreciated:
One liner version(?):
1
u/Nevindy Jun 09 '16
Python
1
u/[deleted] Jun 10 '16 edited Jun 10 '16
Java
First time. Worked hard to get it. I make some assumptions in my code, which I am not sure are allowed. For example, if an entire line is blank, I assume, that there is a string with no words: " " . I also assume that the input text to be transposed, has well defined line breaks indicated by \n. Please let me know if you think any of the assumptions are a bit too much. Thanks.
package transpose;
import java.util.ArrayList;
public class Transpose {
}
1
u/StickMan77 Jun 10 '16
Java:
1
u/ApatheticWrath Jun 10 '16
Java
Any feedback appreciated but bear in mind I just started learning coding like 6 months ago so it maaaaay go over my head.
1
u/boiling_tunic Jun 11 '16
Ruby
Input is in
text1
u/[deleted] Jun 11 '16 edited Jun 11 '16
Ruby
I am new to Ruby so any feedback as to how I could refactor this code would be appreciated!
1
u/vesche Jun 11 '16
Python 3
1
u/i_luv_food Jun 11 '16
Okay, I decided that I'm going to figure out how to program... So here goes. (Python) - I've just seen the various other solutions being posted here and I'll try to make sense of some of it... esp the one liners. Being a noob is rough! Feedback appreciated. First time using python.
Open file, copy input to an array
currentfile = open("testCase2","r") arrayofWords = currentfile.readlines() currentfile.close() array_return = []
Count max length and # of lines
max_length = 0 array_length = len(arrayofWords)
print "Input array"
for i in range(0,len(arrayofWords)): arrayofWords[i] = arrayofWords[i].strip("\n") if(len(arrayofWords[i]) > max_length): max_length = len(arrayofWords[i])
Set up the answer array
for i in range (0, max_length): array_return.append("")
Take the first line, count up to max length
Take the first line, and append each chr it onto the array
for j in range (0, array_length): for i in range (0, max_length): if i >= len(arrayofWords[j]): array_return[i] = array_return[i] + " " else: array_return[i] = array_return[i] + arrayofWords[j][i]
for line in array_return: print line
1
u/47756e6f764247 Jun 12 '16
C#
GitHub
I'm new to programming so any critique will be helpful.
1
u/Virzen Jun 12 '16 edited Jun 12 '16
My solution in JavaScript (feedback is appreciated):
CodePen version: http://codepen.io/virzen/pen/VjaeaY
1
u/NotoriousArab Jun 13 '16
C++ 11
1
u/synae Jun 13 '16
I'm trying to learn Nim by doing challenges from this subreddit. My learning approach is to write it out in very simple python (very few libraries, etc) and then translate it to nim. This is my first one. I'd love some feedback on the nim version.
python 3
This is my very-unpythonic solution which I used to flesh out my nim solution, below.
nim
1
u/[deleted] Jun 14 '16
Java:
1
u/TheBlackCat13 Jun 14 '16 edited Jun 14 '16
Python 3 two-liner (including imports). Since it didn't specify how the number of lines was determined, the first input sets the number of lines. Feedback welcome.
Edit: If we are using a file as the input, as I see some other people are doing, here is the two-liner:
1
u/j4yne Jun 15 '16
Ruby. I'm new, so any feedback welcome.
To summarize, my solution was to throw the input text into a multidimensional array, square the array off, and call Array#transpose on it. If there's a shorter/better way, please feel free to let me know:
1
u/jpan127 Jun 16 '16
First post can anyone please critique? Not sure how to simplify or circumvent the try/except block for when some lines are longer than others.
Python 3
1
u/4kpics Jun 17 '16
Python 3
1
u/Gobbedyret 1 0 Jun 19 '16 edited Jun 19 '16
Python 3.5
Sometimes, using Python feels like cheating. This is almost
import solution.1
u/KingRodian Jun 20 '16 edited Jun 20 '16
C++, guess it's pretty long-winded.(also, I'm a noob) I have the input premade as a vector, so I guess I'm not really doing it right, but it would take few changes to take a file or getlines from standard input.
Edit: changed longestline to return an int instead of taking longest as argument, for clarity.
1
u/dpburst Jun 22 '16
Go (GoLang)
This is coming late but I wanted the solution to be UTF-8 compliant.
Input
Output
Input (with unicode characters)
echo -e '🍺eer\nfoo\nbar\nquux' | go run tt.goOutput
1
u/ProbablyStoned0x1A4 Jun 24 '16
Really late to the party, but I thought this would be a good challenge to write my first ever Go program.
Nothing fancy, but it gets the job done.
1
u/mhornberger Jun 26 '16
Python3
Output:
1
u/Escherize Jun 27 '16
Clojure
1
u/avo_cantigas Jun 28 '16
Python 3.5
1
u/[deleted] Jul 01 '16 edited Jul 01 '16
Python 3
Decided not to just copy it into a list, that method would be good for small files but larger ones would eat up way too much memory. Used this method instead where It reads from the file and writes to the file without having to store as much memory.
In order for this program to work you need a exampleR.txt file in the same folder as this program that has text in it. After you do that, all you should need to do is have python 3 installed and run the program!
Output:
1
u/dukeval82 Jul 03 '16
Python First attempt, all comments are welcome. I know doing it in memory may not be the best for a real world scenario.
1
u/EtDecius Jul 07 '16
C++: Creates a new string with transposed text, then prints it.
1
u/sdlambert Jul 27 '16
Solution in Javascript (Node)
1
u/yoavst Aug 06 '16
Kotlin
1
u/Zambito1 Sep 06 '16 edited Sep 06 '16
Java
+/u/CompileBot Java
1
u/FlammableMarshmallow Jun 07 '16
Python3.5
This was a really easy challenge in Python, seeing as the transpose function in python is just
zip(*iterable).9
u/[deleted] Jun 06 '16 edited Jun 06 '16
[removed] — view removed comment