Write a function that takes a list of strings an prints them, one per line, in a rectangular frame. For example the list ["Hello", "World", "in", "a", "frame"] gets printed as:
*********
* Hello *
* World *
* in *
* a *
* frame *
*********
*********
* Hello *
* World *
* in *
* a *
* frame *
*********
Asterisks need to be inline
>>61523598
You can't be serious this is shit easy goy
Iterate over all elements to find the max length. Then pad all the shorter strings with the required number of spaces to match that length. Pretty simple
>>61523598
>Find the widest string.
>Format the rest of the prints to be the same width as widest string
Wow that was hard
Ask /sci/, they are the homework board.
>>61523598max_len = len(max(mah_list, key=len)) + 2
for i in mah_list:
if i == mah_list[0]:
print(max_len * '*')
print('*' + i + (' ' * (max_len - (len(i) - 1))) + '*')
Not sure if it works.
t.retard on the phone
>>61523740
>>61523748
>>61523777
>>61523787
I have already done it guys. I just want to see what it would look like in other languages.
>>61523619
>>61523598
use code tags retard
>>61523740
>>61523748
this
>>61523841print("Dont be so angry anon")
>>61523794
Kek
Remove that print '*' from loop and add one before and after.
t.same phone retard
>>61523941
>>61523794
He doesn't write code on his phone. Ahaha oh wow.
>>61523968
I might be stupid, but not that stupid...
>>61523968
>>61523987
Or better yet, I don't hate myself that much.
#!/bin/bash
frame_start() {
declare -i i
for (( i=0; i<$1; i++ )); do
echo -n '*'
done
echo
}
# find max len
declare -i max_len
for word in "$@"; do
if (( ${#word} > max_len )); then
max_len=${#word}
fi
done
frame_start "$(( max_len + 4 ))"
printf '* %'"$max_len"'s *\n' "$@"
frame_start "$(( max_len + 4 ))"
it's not left justified but whatever, sue me. it would just mean another for loop and some printf indecipherable formatting cancer like %s%-*i "string" some_num.
>>61523968
That is really poorly programmed.
count=0
for str in ["Hello", "World", "in", "a", "frame"]:
if len(str)>0 :
count=count+1
k=9-len(str)
# print(k)
if count==1:
print("***********")
print("*"+str+(" "*k)+"*" )
print("***********")
It just werks :^)
public class StringRectangle {
public static void main(String[] args) {
int max = 0;
for(int i = 0;i<args.length;i++)
max = args[i].length() > max ? args[i].length():max;
for(int i = 0;i<max+4;i++)
System.out.print('*');
for(int i = 0;i<args.length;i++)
System.out.printf("\n* %-"+max+"s *",args[i]);
System.out.println();
for(int i = 0;i<max+4;i++)
System.out.print('*');
System.out.println();
}
}
>>61524032
Yeah i know, its it's pretty fucking bad. It works though, and that counts for something. I guess.
>>61524090import java.util.Scanner;
public class Bin2Dec {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a binary number for it's decimal equivalent: ");
String number = input.nextLine();
System.out.println(bin2Dec(number));
}
public static int bin2Dec(String number) {
int result = 0;
for(int i = 0;i<number.length();i++)
result = (result<<1) + number.charAt(i)-'0';
return result;
}
}
#!/usr/bin/env ruby
def encase(words)
max_length = words.max_by {|w| w.length }.length
puts '*'*(max_length + 4)
words.each {|w| puts "* #{w.ljust(max_length)} *" }
puts '*'*(max_length + 4)
end
encase(ARGV)$ ./encase.rb hello world in a frame
*********
* hello *
* world *
* in *
* a *
* frame *
*********
Bumping for other languages
printf "* %-${max_len}s *\n" "$@"
#!/usr/bin/python3
import sys
s=sys.argv[1:]
ml=max([len(x) for x in s])
f="*"*(ml+2)
print(f+"\n"+"\n".join(["*"+x+" "*(ml-len(x))+"*" for x in s])+"\n"+f)
Not sure if it can be even more shortened.
>>61523787
KISAMAAA
C
for some reason my code is blocked.
https://hastebin.com/qagicuguda.c