Question

Create a graph in python for a given input series without using any libraries

I was asked this question:

Given any input series a corresponding graph must be generated without the use of any libraries.

After, trying my best, I arrived at this solution to which they replied it had a logical issue.

# Create the matrix
print("Enter the sequence with spaces: ")
arr = list(map(int, input().split()))
count = len(arr)
rows = int(sum(arr)) 
cols = int(sum(arr) + 4)
content = [[" "]*cols for _ in range(rows)]
maxq = 0
maxp = 0
content[0][0] = "/"

# Apply the positions in the matrix
p = 0
q = arr[0]
k = 0
for l in range(q):
    if (k != q):
        content[k][p] = "/"
        p = p + 1
        k = k + 1
p = q
flag = 1
i = 0
j = 0
k = 0
c = 0
temp = 0
r = 0
flag = 1
for i,j in enumerate(arr):
    c = c + 1
    if c < count:
        k = arr[i+1]
    else:
        k = 0
    if arr[i]:
        if flag == 1:
            content[q][p] = "/\\"
            if maxq < q:
                maxq = q
                maxp = p
            qori = q
            pori = p
            p = p + k
            temp = q - k
            if temp < 0:
                q = q + k
                for l in range(k):
                    if ((qori != q) and (pori != p)):
                        qori = qori + 1
                        pori = pori + 1
                        content[qori][pori] = "/"
                        flag = 1
            else:
                q = q - k
                for l in range(k):
                    if (((qori != q) or (qori > 0)) and ((pori != p) or (pori > 0))):
                        qori = qori - 1
                        pori = pori + 1
                        content[qori][pori] = "\\"
                        flag = 2
        
        elif flag == 2:
            content[q][p] = "\/"
            if content[q-1][p-1] == "/":
                content[q][p] = "/\\"
            if maxq < q:
                maxq = q
                maxp = p
            qori = q
            pori = p
            q = q + k
            p = p + k
            for l in range(k):
                if ((qori != q) and (pori != p)):
                    qori = qori + 1
                    pori = pori + 1
                    content[qori][pori] = "/"
            flag = 1


# Apply the top data
content[maxq][maxp] = "<O>"


# build frame
width       = len(str(max(rows,cols)-1))
contentLine = "# | values |"
dashes      = "-".join("-"*width for _ in range(cols))
frameLine   = contentLine.replace("values",dashes)
frameLine   = frameLine.replace("#"," "*width)
frameLine   = frameLine.replace("| ","+-").replace(" |","-+")


# print grid
print(frameLine)
for i,row in enumerate(reversed(content),1):
    values = " ".join(f"{v:{width}s}" for v in row)
    line   = contentLine.replace("values",values)
    line   = line.replace("#"," ")
    print(line)
print(frameLine)


# x-axis
numLine = contentLine.replace("|"," ")
numLine = numLine.replace("#"," "*width)
numLine = numLine.replace("values"," ")
print(numLine)

I was given this as a hint/reference to solve my logical issue:

#!/usr/bin/env python
# coding: utf-8
num = input("Enter the number of elements")
a= list()
for i in range(int(num)):
    n = input()
    a.append(int(n))
# In[6]:
b=[]
g=0
m=0
mini=0
mi=-1
c=0
for i in a:
    for j in range(i):
        if g%2==0:
            b.append(1)
            c=c+1
            if c>m:
                m=c
                mi=len(b)-1        
        else:
            b.append(-1)
            c=c-1
            if c<mini:
                mini = c
    g=g+1
# In[7]:
ans=[[' ' for i in range(len(b)+1)] for j in range(m+3-mini)]
g=0
h=-mini
for i in range(len(b)):
    t=0
    if i>mi:
       t=1 
    if b[i]==1:
        ans[2+-mini+m-h][i+t]='/'
#         print(i,h)
#         print(ans[i][h])
        h=h+1
    else:
        h=h-1
        ans[2+m-h-mini][i+t]="\\"
# ans
print(len(b))
ans[0][mi+1]='o'
ans[1][mi]='/'
ans[1][mi+2]='\\'
ans[1][mi+1]='|'
ans[2][mi]='<'
ans[2][mi+2]='>'
for i in ans:
    for j in i:
        print(j,end="")
    print('')






1 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 5 more requests to produce the answer.

5 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Create a graph in python for a given input series without using any libraries
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <...

    C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <iostream> #include <fstream> #include <string> using namespace std; int measureElementsPerLine(ifstream& inFile) {    // Add your code here.    string line;    getline(inFile, line);    int sp = 0;    for (int i = 0; i < line.size(); i++)    {        if (line[i] == ' ')            sp++;    }    sp++;    return sp; } int measureLines(ifstream& inFile) {    // Add your code here.    string line;    int n = 0;    while (!inFile.eof())    {        getline(inFile,...

  • without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr,...

    without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr, low, high, x): # Check base case if low > high : return None else: mid = (high + low) // 2 element arr[mid] == X: if element return mid elif element > X: return binary-search(arr, low, mid 1, x) else: return binary_search(arr, mid + 1, high, x) Selection Sort: def selection_sort (arr): for i in range (len(arr)): smallest index = i smallest value...

  • Need help with my ksmall program. I get an error saying segmentation dump. Thanks #include<iostream> using...

    Need help with my ksmall program. I get an error saying segmentation dump. Thanks #include<iostream> using namespace std; int ksmall(int*, int, int , int); void swap(int*, int*); int main() {     int SIZE = 10;     int target;     int begining=0;     int ending=SIZE-1;     int *array1= new int[SIZE];     cout << "Enter 10 integers: " << endl;     for (int i=0; i<SIZE; i++)     {        cin>>array1[i];     }     cout << " What is the Kth smallest number...

  • Below is my code please help me edit it so in the beginning it ask for Press any key to start Tas...

    below is my code please help me edit it so in the beginning it ask for Press any key to start Task n, where n is the task number (1, 2, or 3,4). I already wrote the code for the 4 task. Also please write it so when 1 code is finish running it return to the prompt where it ask to run another task #task 1 list1 = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','.',',','?'] morse = ['.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','-----','.----','..---','...--','....-','.....','-....','--...','---..','----.','.-.-.-','--..--','..--..'] inp = input("Enter original text : ")...

  • howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE]...

    howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8};    int index;    index=0;    res = values[index];    for (int j=1; j<SIZE; j++)    {        if (values[j] > res)        {            res = values[j];            index = j;        cout << index << res << endl;        }    }    cout <<...

  • #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include<time.h> void...

    #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include<time.h> void insertionSort(int arr[], int n); void merge(int a[], int l1, int h1, int h2); void mergeSort(int a[], int l, int h) { int i, len=(h-l+1); //Using insertion sort for small sized array if (len<=5) { insertionSort(a+l, len); return; } pid_t lpid,rpid; lpid = fork(); if(lpid<0) { //Lchild proc not created perror("Left Child Proc. not created\n"); _exit(-1); } else if (lpid==0) { mergeSort(a,l,l+len/2-1); _exit(0); } else...

  • this python code below is to find the longest common subsequence from two input string !!!...

    this python code below is to find the longest common subsequence from two input string !!! however it give me the wrong answer, can any one fix it thanks def lcs(s1, s2): matrix = [["" for x in range(len(s2))] for x in range(len(s1))] for i in range(len(s1)): for j in range(len(s2)): if s1[i] == s2[j]: if i == 0 or j == 0: matrix[i][j] = s1[i] else: matrix[i][j] = matrix[i-1][j-1] + s1[i] else: matrix[i][j] = max(matrix[i-1][j], matrix[i][j-1], key=len) cs =...

  • Add reverse() method which reverses the content of array without using additional array. rotate(k) method which...

    Add reverse() method which reverses the content of array without using additional array. rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete is successful returns 1, otherwise 0; * for successful deletion:...

  • C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want...

    C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT