Header Ad

Leetcode Transpose File problem solution

In this Leetcode Transpose File problem solution, we have Given a text file file.txt, transpose its content. You may assume that each row has the same number of columns, and each field is separated by the ' ' character.

Leetcode Transpose File problem solution


First Problem solution.

#!/bin/bash

declare -a names
declare -a ages

while read line
do 
	words=($line)
	names+=(${words[0]})
	ages+=(${words[1]})
done < file.txt

for name in ${names[@]}
do 
	printf "%s " $name
done

printf "\n"

for age in ${ages[@]}
do 
	printf "%s " $age
done



Second Problem solution.

cat file.txt | head -n1 | awk '{print NF}' | xargs seq 1 \
  | xargs -I {} sh -c \
  "cat file.txt | awk '{print \${}}' | tr '\n' ' '  | sed -E 's/\ +$//g' && echo"


Post a Comment

0 Comments