Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
leetcode
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Philipp
leetcode
Commits
a752c4f1
Commit
a752c4f1
authored
2 years ago
by
Philipp
Browse files
Options
Downloads
Patches
Plain Diff
LC '194. Transpose File' solution
parent
72d71fce
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
.gitignore
+1
-1
1 addition, 1 deletion
.gitignore
194-file.txt
+4
-0
4 additions, 0 deletions
194-file.txt
194-transpose-file.sh
+23
-0
23 additions, 0 deletions
194-transpose-file.sh
with
28 additions
and
1 deletion
.gitignore
+
1
−
1
View file @
a752c4f1
194-file-transposed.txt
This diff is collapsed.
Click to expand it.
194-file.txt
0 → 100644
+
4
−
0
View file @
a752c4f1
name age gender
alice 21 female
ryan 30 male
steve 14 male
This diff is collapsed.
Click to expand it.
194-transpose-file.sh
0 → 100755
+
23
−
0
View file @
a752c4f1
#!/bin/sh
# solution for
# https://leetcode.com/problems/transpose-file/
#export PS4="\$LINENO: "
#set -xv
file
=
"194-file.txt"
transposed_file
=
"194-file-transposed.txt"
# delete file contents
cat
/dev/null
>
$transposed_file
# count ' ' (spaces) in the first line as the are the delimiter
# no trailing spaces assumed
# +1 because there is always one delimiter less than entries
nmb_cols
=
$((
$(
head
-1
194-file.txt |
grep
-o
" "
|
wc
-l
)
+
1
))
# iterate over evey column
for
i
in
$(
seq
1
$nmb_cols
)
;
do
# extract column, replace newlines with spaces, append to output file
cut
-d
" "
-f
$i
<
$file
|
tr
"
\n
"
" "
>>
$transposed_file
# append newline for proper formatting
echo
""
>>
$transposed_file
done
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment