'''
Created on 20-Jan-2022

@author: bthakur
'''

import inspect
import traceback
import random
import numpy as np
from sympy import (latex, Symbol, symbols,
                   )

from xv.util import toIntOrFloat, listAttr
from xv.km.storeddata import BaseStoredQuestions

# _assorted_stored_questions
# AssortedStoredQuestions

class AssortedStoredQuestions(BaseStoredQuestions):
    _QUESTION_KEYWORDS = None
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        pass
    
    def get_question_subsets_of_n_elements(self):
        
        keywords = [
            'assorted', 'subsets', 'n elements', 'set',
            ]
        
        prob_s = rf"""
        Prove that a set of $n$ different elements have exactly $2^{{n}}$ different subsets.
                
        """
        
        soln_s = rf"""
        We can choose each element or leave them. In a set, the order of elements doesn't matter.
        Thus each element can be selected or left out to form a subset, the number of subsets will be:
        $\displaystyle \left( ^{{2}}C_{{1}} \right)^{{n}} = 2^n$ 
        """
        
        answer_s = rf"""
        $2^n$

        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass
    
    
    def get_question_solve_for_x_type_1(self):
        
        keywords = [
            'assorted', 'solve', 'solve for x',
            ]
        
        prob_s = rf"""
        Let $\displaystyle 
        x_{{1}}, x_{{2}}, x_{{3}}, \ldots
        $ be a sequence of nonzero real numbers satisfying
        
        $$\displaystyle 
        x_{{n}}=\frac{{x_{{n-2}} x_{{n-1}}}}{{2 x_{{n-2}}-x_{{n-1}}}}, \quad n=3,4,5, \ldots .
        $$
        Establish necessary and sufficient conditions on $x_{{1}}$ and $x_{{2}}$ for $x_{{n}}$ to be an integer for infinitely many values of $n$.
                        
        """
        
        soln_s = rf"""
        Let us substitute value for $n$ to get terms in as functions of $x_{{1}}$ and $x_{{2}}$. 
        
        $$
        \begin{{aligned}}
        &x_{{3}}=\frac{{x_{{1}} x_{{2}}}}{{2 x_{{1}}-x_{{2}}}}, \\
        &x_{{4}}=\frac{{x_{{1}} x_{{2}}}}{{3 x_{{1}}-2 x_{{2}}}}, \\
        &x_{{5}}=\frac{{x_{{1}} x_{{2}}}}{{4 x_{{1}}-3 x_{{2}}}} \\
        &\cdots
        \end{{aligned}}
        $$
        As we notice, we can write:
        
        $$
        x_{{n}}=\frac{{x_{{1}} x_{{2}}}}{{(n-1) x_{{1}}-(n-2) x_{{2}}}}
        $$
        $$\Rightarrow
        x_{{n}}=\frac{{x_{{1}} x_{{2}}}}{{\left(x_{{1}}-x_{{2}}\right) n+\left(2 x_{{2}}-x_{{1}}\right)}}
        $$
        if $x_{{1}} = x_{{2}}$:
        $$\Rightarrow
        x_{{n}} = x_{{1}} = x_{{1}} \text{{ is integer for all values of }} {{n}}
        $$ 
        if $x_{{1}} \neq x_{{2}}$:
        $\Rightarrow
        \left| \left(x_{{1}}-x_{{2}}\right) n \right| 
        $ is an increasing function of ${{n}}$ and other terms are constant.
        Hence, $\left| x_{{n}} \right|$ is a decreasing function and at some value $
        {{n}} > {{r}}$ it becomes smaller than 1. So $x_{{n}}$ then will no longer be an integer. Hence, this does not satisfy the specified condition.

        So, the necessary and sufficient condition for $x_{{n}}$ to be integer is $
        x_{{1}} = x_{{1}} $. 
        """
        
        answer_s = rf"""
        $x_{{1}} = x_{{1}} $
        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass
    
    def get_question_maximize_pos_nums(self):
        
        keywords = [
            'assorted', 'maximize', 'positive numbers',
            ]
        
        prob_s = rf"""
        Find positive numbers $n$ and $a_{{1}}, a_{{2}}, \ldots, a_{{n}}$ such that $a_{{1}}+\cdots+a_{{n}}$ $=1000$ and the product $a_{{1}} a_{{2}} \cdots a_{{n}}$ is as large as possible.
                        
        """
        
        soln_s = rf"""
        Let us find simple cases with sum as smaller number and the best solution.
        At sum = 2, 1 + 1 = 2, max product 1 * 1 = 1
        At sum = 3, 2 + 1 = 2, max product 2 * 1 = 2
        At sum = 4, 2 + 2 = 4, max product 2 * 2 = 4
        At sum = 5, 3 + 2 = 5, max product 3 * 2 = 6
        At sum = 6, 3 + 3 = 6, max product 3 * 3 = 9
        At sum = 7, 3 + 2 + 2 = 7, max product 3 * 2 * 2 = 12
        At sum = 8, 3 + 3 + 2 = 8, max product 3 * 3 * 2 = 18
        At sum = 9, 3 + 3 + 3 = 9, max product 3 * 3 * 3 = 27
        At sum = 10, 3 + 3 + 2 + 2 = 10, max product 3 * 3 * 2 * 2 = 36
        At sum = 11, 3 + 3 + 3 + 2 = 11, max product 3 * 3 * 3 * 2 = 54
        At sum = 12, 3 + 3 + 3 + 3 = 12, max product 3 * 3 * 3 * 3 = 81
        At sum = 13, 3 + 3 + 3 + 2 + 2 = 13, max product 3 * 3 * 3 * 2 * 2 = 108
        At sum = 14, 3 + 3 + 3 + 3 + 2 = 10, max product 3 * 3 * 3 * 3 * 2 = 162
        ...
        
        At sum = 1000, 3 + 3 + ... + 3 + 3 + 2 + 2, max product $3^{{332}} \times 2^{{2}}$
        """
        
        answer_s = rf"""
        $3^{{332}} \times 2^{{2}}$
        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass
    
    pass
#class AssortedStoredQuestions:
